]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/visitor.cpp
Minor cleanups and tweaks
[libs/gl.git] / source / glsl / visitor.cpp
1 #include <msp/core/raii.h>
2 #include "visitor.h"
3
4 using namespace std;
5
6 namespace Msp {
7 namespace GL {
8 namespace SL {
9
10 void TraversingVisitor::visit(Block &block)
11 {
12         if(&block!=current_block)
13                 enter(block);
14         SetForScope<Block *> set_block(current_block, &block);
15         for(const RefPtr<Statement> &s: block.body)
16                 s->visit(*this);
17 }
18
19 void TraversingVisitor::visit(RefPtr<Expression> &expr)
20 {
21         expr->visit(*this);
22 }
23
24 void TraversingVisitor::visit(MemberAccess &memacc)
25 {
26         visit(memacc.left);
27 }
28
29 void TraversingVisitor::visit(Swizzle &swizzle)
30 {
31         visit(swizzle.left);
32 }
33
34 void TraversingVisitor::visit(UnaryExpression &unary)
35 {
36         visit(unary.expression);
37 }
38
39 void TraversingVisitor::visit(BinaryExpression &binary)
40 {
41         visit(binary.left);
42         visit(binary.right);
43 }
44
45 void TraversingVisitor::visit(Assignment &assign)
46 {
47         visit(assign.left);
48         visit(assign.right);
49 }
50
51 void TraversingVisitor::visit(TernaryExpression &ternary)
52 {
53         visit(ternary.condition);
54         visit(ternary.true_expr);
55         visit(ternary.false_expr);
56 }
57
58 void TraversingVisitor::visit(FunctionCall &call)
59 {
60         for(RefPtr<Expression> &a: call.arguments)
61                 visit(a);
62 }
63
64 void TraversingVisitor::visit(ExpressionStatement &expr)
65 {
66         visit(expr.expression);
67 }
68
69 void TraversingVisitor::visit(InterfaceLayout &layout)
70 {
71         layout.layout.visit(*this);
72 }
73
74 void TraversingVisitor::visit(StructDeclaration &strct)
75 {
76         strct.members.visit(*this);
77 }
78
79 void TraversingVisitor::visit(VariableDeclaration &var)
80 {
81         if(var.layout)
82                 var.layout->visit(*this);
83         if(var.init_expression)
84                 visit(var.init_expression);
85         if(var.array_size)
86                 visit(var.array_size);
87 }
88
89 void TraversingVisitor::visit(InterfaceBlock &iface)
90 {
91         if(iface.layout)
92                 iface.layout->visit(*this);
93         if(iface.members)
94                 iface.members->visit(*this);
95 }
96
97 void TraversingVisitor::visit(FunctionDeclaration &func)
98 {
99         enter(func.body);
100         SetForScope<Block *> set_block(current_block, &func.body);
101         for(const RefPtr<VariableDeclaration> &p: func.parameters)
102                 p->visit(*this);
103         func.body.visit(*this);
104 }
105
106 void TraversingVisitor::visit(Conditional &cond)
107 {
108         visit(cond.condition);
109         cond.body.visit(*this);
110         cond.else_body.visit(*this);
111 }
112
113 void TraversingVisitor::visit(Iteration &iter)
114 {
115         enter(iter.body);
116         SetForScope<Block *> set_block(current_block, &iter.body);
117         if(iter.init_statement)
118                 iter.init_statement->visit(*this);
119         if(iter.condition)
120                 visit(iter.condition);
121         iter.body.visit(*this);
122         if(iter.loop_expression)
123                 visit(iter.loop_expression);
124 }
125
126 void TraversingVisitor::visit(Passthrough &pass)
127 {
128         if(pass.subscript)
129                 visit(pass.subscript);
130 }
131
132 void TraversingVisitor::visit(Return &ret)
133 {
134         if(ret.expression)
135                 visit(ret.expression);
136 }
137
138
139 void NodeRemover::apply(Stage &s, const set<Node *> &tr)
140 {
141         stage = &s;
142         to_remove = &tr;
143         s.content.visit(*this);
144 }
145
146 template<typename T>
147 void NodeRemover::remove_from_map(map<string, T *> &vars, const string &key, T &node)
148 {
149         auto i = vars.find(key);
150         if(i!=vars.end() && i->second==&node)
151                 vars.erase(i);
152 }
153
154 void NodeRemover::visit(Block &block)
155 {
156         SetForScope<Block *> set_block(current_block, &block);
157         for(auto i=block.body.begin(); i!=block.body.end(); )
158         {
159                 (*i)->visit(*this);
160                 if(to_remove->count(i->get()))
161                         block.body.erase(i++);
162                 else
163                         ++i;
164         }
165 }
166
167 void NodeRemover::visit(TypeDeclaration &type)
168 {
169         if(to_remove->count(&type))
170                 remove_from_map(stage->types, type.name, type);
171 }
172
173 void NodeRemover::visit(VariableDeclaration &var)
174 {
175         if(recursive_remove || to_remove->count(&var))
176         {
177                 remove_from_map(current_block->variables, var.name, var);
178                 stage->locations.erase(var.name);
179                 if(var.linked_declaration)
180                         var.linked_declaration->linked_declaration = 0;
181         }
182         else if(var.init_expression && to_remove->count(var.init_expression.get()))
183                 var.init_expression = 0;
184 }
185
186 void NodeRemover::visit(InterfaceBlock &iface)
187 {
188         if(to_remove->count(&iface))
189         {
190                 remove_from_map(stage->interface_blocks, format("%s %s", iface.interface, iface.block_name), iface);
191                 if(!iface.instance_name.empty())
192                         remove_from_map(stage->interface_blocks, iface.instance_name, iface);
193         }
194         SetFlag set_recursive(recursive_remove, recursive_remove || to_remove->count(&iface));
195         TraversingVisitor::visit(iface);
196 }
197
198 void NodeRemover::visit(FunctionDeclaration &func)
199 {
200         if(to_remove->count(&func))
201         {
202                 remove_from_map(stage->functions, func.name, func);
203                 if(!func.signature.empty())
204                         remove_from_map(stage->functions, func.name+func.signature, func);
205         }
206         TraversingVisitor::visit(func);
207 }
208
209 void NodeRemover::visit(Iteration &iter)
210 {
211         if(to_remove->count(iter.init_statement.get()))
212                 iter.init_statement = 0;
213         TraversingVisitor::visit(iter);
214 }
215
216
217 void NodeReorderer::apply(Stage &stage, Node &before, const set<Node *> &tr)
218 {
219         reorder_before = &before;
220         to_reorder = &tr;
221         stage.content.visit(*this);
222 }
223
224 void NodeReorderer::visit(Block &block)
225 {
226         auto insert_point = block.body.end();
227         for(auto i=block.body.begin(); i!=block.body.end(); )
228         {
229                 (*i)->visit(*this);
230                 if(insert_point!=block.body.end() && to_reorder->count(i->get()))
231                         block.body.splice(insert_point, block.body, i++);
232                 else
233                 {
234                         if(i->get()==reorder_before)
235                                 insert_point = i;
236                         ++i;
237                 }
238         }
239 }
240
241 } // namespace SL
242 } // namespace GL
243 } // namespace Msp