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