X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fglsl%2Fvisitor.h;h=e0d74f6226927299913e3e9d41892042eb52f6bb;hb=518f751d385b733adbf43fe4056403740709edec;hp=582628bbedc49ecfe695c9eacd7909d861bfa071;hpb=696a97bd7411d69953c1a9e4b5f3dfb4c1d848f1;p=libs%2Fgl.git diff --git a/source/glsl/visitor.h b/source/glsl/visitor.h index 582628bb..e0d74f62 100644 --- a/source/glsl/visitor.h +++ b/source/glsl/visitor.h @@ -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,65 +71,60 @@ public: virtual void visit(Return &); }; -class StageVisitor: public TraversingVisitor +/** Gathers nodes of a particular type from the syntax tree. */ +template +class NodeGatherer: private TraversingVisitor { -public: - typedef void ResultType; - -protected: - Stage *stage; - - StageVisitor(); +private: + std::vector nodes; public: - virtual void apply(Stage &); - void get_result() const { } + const std::vector &apply(Stage &s) { s.content.visit(*this); return nodes; } + +private: + virtual void visit(T &n) { nodes.push_back(&n); } }; -class BlockModifier: public StageVisitor +/** Removes a set of nodes from the syntax tree. */ +class NodeRemover: private TraversingVisitor { -protected: - bool remove_node; - std::vector > insert_nodes; - - BlockModifier(); - - void flatten_block(Block &); - void apply_and_increment(Block &, NodeList::iterator &); +private: + Stage *stage; + const std::set *to_remove; + bool recursive_remove; public: - using StageVisitor::visit; - virtual void visit(Block &); -}; + NodeRemover(); -template -class NodeGatherer: public StageVisitor -{ -public: - typedef std::vector ResultType; + void apply(Stage &, const std::set &); private: - std::vector nodes; + template + void remove_from_map(std::map &, const std::string &, T &); -public: - const ResultType &get_result() const { return nodes; } - using StageVisitor::visit; - virtual void visit(T &n) { nodes.push_back(&n); } + virtual void visit(Block &); + virtual void visit(StructDeclaration &); + virtual void visit(VariableDeclaration &); + virtual void visit(InterfaceBlock &); + virtual void visit(FunctionDeclaration &); + virtual void visit(Iteration &); }; -class NodeRemover: public StageVisitor +/** 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: - std::set to_remove; + Node *reorder_before; + const std::set *to_reorder; public: - NodeRemover() { } - NodeRemover(const std::set &); + NodeReorderer(); - using StageVisitor::visit; + void apply(Stage &, Node &, const std::set &); + +private: virtual void visit(Block &); - virtual void visit(VariableDeclaration &); - virtual void visit(Iteration &); }; } // namespace SL