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