]> git.tdb.fi Git - libs/gl.git/blobdiff - source/programcompiler.h
Move unused aggregate detection to UnusedVariableDetector
[libs/gl.git] / source / programcompiler.h
index ecae3dc13411273ec80a825b2a79e780712fae34..829f75358cdd08695d16730184f109cc03e5c0d6 100644 (file)
@@ -12,8 +12,17 @@ class Program;
 
 class ProgramCompiler
 {
-public:
-       struct Formatter: ProgramSyntax::NodeVisitor
+private:
+       struct Visitor: ProgramSyntax::TraversingVisitor
+       {
+               ProgramSyntax::Stage *stage;
+
+               Visitor();
+
+               virtual void apply(ProgramSyntax::Stage &);
+       };
+
+       struct Formatter: Visitor
        {
                std::string formatted;
                unsigned indent;
@@ -41,8 +50,7 @@ public:
                virtual void visit(ProgramSyntax::Return &);
        };
 
-private:
-       struct VariableResolver: ProgramSyntax::TraversingVisitor
+       struct VariableResolver: Visitor
        {
                std::vector<ProgramSyntax::Block *> blocks;
                ProgramSyntax::StructDeclaration *type;
@@ -59,20 +67,19 @@ private:
                virtual void visit(ProgramSyntax::InterfaceBlock &);
        };
 
-       struct InterfaceGenerator: ProgramSyntax::TraversingVisitor
+       struct InterfaceGenerator: Visitor
        {
-               ProgramSyntax::Stage *stage;
                std::string in_prefix;
                std::string out_prefix;
                unsigned scope_level;
-               std::map<std::string, ProgramSyntax::NodePtr<ProgramSyntax::Node> > iface_declarations;
+               std::map<std::string, ProgramSyntax::VariableDeclaration *> iface_declarations;
                bool remove_node;
-               std::list<ProgramSyntax::NodePtr<ProgramSyntax::Node> > insert_nodes;
+               std::list<ProgramSyntax::Node *> insert_nodes;
 
                InterfaceGenerator();
 
                static std::string get_out_prefix(ProgramSyntax::StageType);
-               void visit(ProgramSyntax::Stage &);
+               virtual void apply(ProgramSyntax::Stage &);
                virtual void visit(ProgramSyntax::Block &);
                std::string change_prefix(const std::string &, const std::string &) const;
                bool generate_interface(ProgramSyntax::VariableDeclaration &, const std::string &, const std::string &);
@@ -82,45 +89,41 @@ private:
                virtual void visit(ProgramSyntax::Passthrough &);
        };
 
-       struct VariableRenamer: ProgramSyntax::TraversingVisitor
+       struct VariableRenamer: Visitor
        {
                virtual void visit(ProgramSyntax::VariableReference &);
                virtual void visit(ProgramSyntax::VariableDeclaration &);
        };
 
-       struct UnusedVariableLocator: ProgramSyntax::TraversingVisitor
+       struct UnusedVariableLocator: Visitor
        {
-               ProgramSyntax::Stage *stage;
                std::set<ProgramSyntax::Node *> unused_nodes;
+               std::map<ProgramSyntax::VariableDeclaration *, ProgramSyntax::Node *> aggregates;
+               ProgramSyntax::Node *aggregate;
                std::map<ProgramSyntax::VariableDeclaration *, ProgramSyntax::Node *> assignments;
                bool assignment;
+               bool record_target;
                ProgramSyntax::VariableDeclaration *assignment_target;
+               bool indeterminate_target;
+               bool self_referencing;
 
                UnusedVariableLocator();
 
-               void visit(ProgramSyntax::Stage &);
                virtual void visit(ProgramSyntax::VariableReference &);
                virtual void visit(ProgramSyntax::MemberAccess &);
                virtual void visit(ProgramSyntax::BinaryExpression &);
                virtual void visit(ProgramSyntax::ExpressionStatement &);
+               virtual void visit(ProgramSyntax::StructDeclaration &);
                virtual void visit(ProgramSyntax::VariableDeclaration &);
+               virtual void visit(ProgramSyntax::InterfaceBlock &);
        };
 
-       struct NodeRemover: ProgramSyntax::TraversingVisitor
+       struct NodeRemover: Visitor
        {
-               ProgramSyntax::Stage *stage;
                std::set<ProgramSyntax::Node *> to_remove;
-               unsigned n_removed;
-               bool immutable_block;
-               bool remove_block;
-
-               NodeRemover();
 
-               void visit(ProgramSyntax::Stage &);
                virtual void visit(ProgramSyntax::Block &);
-               virtual void visit(ProgramSyntax::StructDeclaration &);
                virtual void visit(ProgramSyntax::VariableDeclaration &);
-               virtual void visit(ProgramSyntax::InterfaceBlock &);
        };
 
        ProgramParser parser;
@@ -136,10 +139,11 @@ public:
 private:
        void process();
        void generate(ProgramSyntax::Stage &);
-       void optimize(ProgramSyntax::Stage &);
+       bool optimize(ProgramSyntax::Stage &);
        static void inject_block(ProgramSyntax::Block &, const ProgramSyntax::Block &);
-       static void resolve_variables(ProgramSyntax::Stage &);
-       std::string format_stage(ProgramSyntax::Stage &);
+       template<typename T>
+       static void apply(ProgramSyntax::Stage &);
+       std::string create_source(ProgramSyntax::Stage &);
 };
 
 } // namespace GL