]> git.tdb.fi Git - libs/gl.git/blobdiff - source/glsl/visitor.h
Store functions definitions in Stage
[libs/gl.git] / source / glsl / visitor.h
index 582628bbedc49ecfe695c9eacd7909d861bfa071..83b310f62ebadbcef621a9b8d1aa65eb571e59c1 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,13 +43,16 @@ public:
        virtual void visit(Jump &) { }
 };
 
+/** An intermediate base visitor class which traverses the syntax tree. */
 class TraversingVisitor: public NodeVisitor
 {
 protected:
-       TraversingVisitor() { }
+       Block *current_block;
+
+       TraversingVisitor(): current_block(0) { }
 
 public:
-       using NodeVisitor::visit;
+       virtual void enter(Block &) { }
        virtual void visit(Block &);
        virtual void visit(ParenthesizedExpression &);
        virtual void visit(MemberAccess &);
@@ -66,64 +71,42 @@ public:
        virtual void visit(Return &);
 };
 
-class StageVisitor: public TraversingVisitor
-{
-public:
-       typedef void ResultType;
-
-protected:
-       Stage *stage;
-
-       StageVisitor();
-
-public:
-       virtual void apply(Stage &);
-       void get_result() const { }
-};
-
-class BlockModifier: public StageVisitor
-{
-protected:
-       bool remove_node;
-       std::vector<RefPtr<Statement> > insert_nodes;
-
-       BlockModifier();
-
-       void flatten_block(Block &);
-       void apply_and_increment(Block &, NodeList<Statement>::iterator &);
-
-public:
-       using StageVisitor::visit;
-       virtual void visit(Block &);
-};
-
+/** Gathers nodes of a particular type from the syntax tree. */
 template<typename T>
-class NodeGatherer: public StageVisitor
+class NodeGatherer: private TraversingVisitor
 {
-public:
-       typedef std::vector<T *> ResultType;
-
 private:
        std::vector<T *> nodes;
 
 public:
-       const ResultType &get_result() const { return nodes; }
-       using StageVisitor::visit;
+       const std::vector<T *> &apply(Stage &s) { s.content.visit(*this); return nodes; }
+
+private:
        virtual void visit(T &n) { nodes.push_back(&n); }
 };
 
-class NodeRemover: public StageVisitor
+/** Removes a set of nodes from the syntax tree. */
+class NodeRemover: private TraversingVisitor
 {
 private:
-       std::set<Node *> to_remove;
+       Stage *stage;
+       const std::set<Node *> *to_remove;
+       bool recursive_remove;
 
 public:
-       NodeRemover() { }
-       NodeRemover(const std::set<Node *> &);
+       NodeRemover();
+
+       void apply(Stage &, const std::set<Node *> &);
+
+private:
+       template<typename T>
+       void remove_from_map(std::map<std::string, T *> &, const std::string &, T &);
 
-       using StageVisitor::visit;
        virtual void visit(Block &);
+       virtual void visit(StructDeclaration &);
        virtual void visit(VariableDeclaration &);
+       virtual void visit(InterfaceBlock &);
+       virtual void visit(FunctionDeclaration &);
        virtual void visit(Iteration &);
 };