]> git.tdb.fi Git - libs/gl.git/blobdiff - source/glsl/visitor.h
Refactor FunctionInliner to do any necessary declaration reordering
[libs/gl.git] / source / glsl / visitor.h
index 2a4ef549b827b4f9b42fb1c7db411770dc92ff21..e0d74f6226927299913e3e9d41892042eb52f6bb 100644 (file)
@@ -9,6 +9,7 @@ namespace Msp {
 namespace GL {
 namespace SL {
 
+/** Base class for all node visitors. */
 class NodeVisitor
 {
 protected:
@@ -20,6 +21,7 @@ public:
        virtual void visit(Literal &) { }
        virtual void visit(ParenthesizedExpression &) { }
        virtual void visit(VariableReference &) { }
+       virtual void visit(InterfaceBlockReference &) { }
        virtual void visit(MemberAccess &) { }
        virtual void visit(UnaryExpression &) { }
        virtual void visit(BinaryExpression &) { }
@@ -41,6 +43,7 @@ public:
        virtual void visit(Jump &) { }
 };
 
+/** An intermediate base visitor class which traverses the syntax tree. */
 class TraversingVisitor: public NodeVisitor
 {
 protected:
@@ -68,6 +71,7 @@ public:
        virtual void visit(Return &);
 };
 
+/** Gathers nodes of a particular type from the syntax tree. */
 template<typename T>
 class NodeGatherer: private TraversingVisitor
 {
@@ -81,6 +85,7 @@ private:
        virtual void visit(T &n) { nodes.push_back(&n); }
 };
 
+/** Removes a set of nodes from the syntax tree. */
 class NodeRemover: private TraversingVisitor
 {
 private:
@@ -94,15 +99,34 @@ public:
        void apply(Stage &, const std::set<Node *> &);
 
 private:
-       void remove_variable(std::map<std::string, VariableDeclaration *> &, VariableDeclaration &);
+       template<typename T>
+       void remove_from_map(std::map<std::string, T *> &, const std::string &, T &);
 
        virtual void visit(Block &);
        virtual void visit(StructDeclaration &);
        virtual void visit(VariableDeclaration &);
        virtual void visit(InterfaceBlock &);
+       virtual void visit(FunctionDeclaration &);
        virtual void visit(Iteration &);
 };
 
+/** Reorders a set of nodes so they appear before another node.  Only nodes
+on the same hierarchly level as the target node are reordered. */
+class NodeReorderer: private TraversingVisitor
+{
+private:
+       Node *reorder_before;
+       const std::set<Node *> *to_reorder;
+
+public:
+       NodeReorderer();
+
+       void apply(Stage &, Node &, const std::set<Node *> &);
+
+private:
+       virtual void visit(Block &);
+};
+
 } // namespace SL
 } // namespace GL
 } // namespace Msp