1 #ifndef MSP_GL_SL_VISITOR_H_
2 #define MSP_GL_SL_VISITOR_H_
12 /** Base class for all node visitors. */
18 virtual ~NodeVisitor() { }
20 virtual void visit(Block &) { }
21 virtual void visit(Literal &) { }
22 virtual void visit(ParenthesizedExpression &) { }
23 virtual void visit(VariableReference &) { }
24 virtual void visit(InterfaceBlockReference &) { }
25 virtual void visit(MemberAccess &) { }
26 virtual void visit(UnaryExpression &) { }
27 virtual void visit(BinaryExpression &) { }
28 virtual void visit(Assignment &) { }
29 virtual void visit(FunctionCall &) { }
30 virtual void visit(ExpressionStatement &) { }
31 virtual void visit(Import &) { }
32 virtual void visit(Precision &) { }
33 virtual void visit(Layout &) { }
34 virtual void visit(InterfaceLayout &) { }
35 virtual void visit(StructDeclaration &) { }
36 virtual void visit(VariableDeclaration &) { }
37 virtual void visit(InterfaceBlock &) { }
38 virtual void visit(FunctionDeclaration &) { }
39 virtual void visit(Conditional &) { }
40 virtual void visit(Iteration &) { }
41 virtual void visit(Passthrough &) { }
42 virtual void visit(Return &) { }
43 virtual void visit(Jump &) { }
46 /** An intermediate base visitor class which traverses the syntax tree. */
47 class TraversingVisitor: public NodeVisitor
52 TraversingVisitor(): current_block(0) { }
55 virtual void enter(Block &) { }
56 virtual void visit(Block &);
57 virtual void visit(ParenthesizedExpression &);
58 virtual void visit(MemberAccess &);
59 virtual void visit(UnaryExpression &);
60 virtual void visit(BinaryExpression &);
61 virtual void visit(Assignment &);
62 virtual void visit(FunctionCall &);
63 virtual void visit(ExpressionStatement &);
64 virtual void visit(InterfaceLayout &);
65 virtual void visit(StructDeclaration &);
66 virtual void visit(VariableDeclaration &);
67 virtual void visit(InterfaceBlock &);
68 virtual void visit(FunctionDeclaration &);
69 virtual void visit(Conditional &);
70 virtual void visit(Iteration &);
71 virtual void visit(Passthrough &);
72 virtual void visit(Return &);
75 /** Gathers nodes of a particular type from the syntax tree. */
77 class NodeGatherer: private TraversingVisitor
80 std::vector<T *> nodes;
83 const std::vector<T *> &apply(Stage &s) { s.content.visit(*this); return nodes; }
86 virtual void visit(T &n) { nodes.push_back(&n); }
89 /** Removes a set of nodes from the syntax tree. */
90 class NodeRemover: private TraversingVisitor
94 const std::set<Node *> *to_remove;
95 bool recursive_remove;
100 void apply(Stage &, const std::set<Node *> &);
104 void remove_from_map(std::map<std::string, T *> &, const std::string &, T &);
106 virtual void visit(Block &);
107 virtual void visit(StructDeclaration &);
108 virtual void visit(VariableDeclaration &);
109 virtual void visit(InterfaceBlock &);
110 virtual void visit(FunctionDeclaration &);
111 virtual void visit(Iteration &);
114 /** Reorders a set of nodes so they appear before another node. Only nodes
115 on the same hierarchly level as the target node are reordered. */
116 class NodeReorderer: private TraversingVisitor
119 Node *reorder_before;
120 const std::set<Node *> *to_reorder;
125 void apply(Stage &, Node &, const std::set<Node *> &);
128 virtual void visit(Block &);