]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/syntax.cpp
Further refactor block and scope management
[libs/gl.git] / source / glsl / syntax.cpp
1 #include "syntax.h"
2 #include "visitor.h"
3
4 using namespace std;
5
6 namespace Msp {
7 namespace GL {
8 namespace SL {
9
10 const Operator Operator::operators[] =
11 {
12         { "[", 2, BINARY, LEFT_TO_RIGHT },
13         { "(", 2, BINARY, LEFT_TO_RIGHT },
14         { ".", 2, BINARY, LEFT_TO_RIGHT },
15         { "++", 2, POSTFIX, LEFT_TO_RIGHT },
16         { "--", 2, POSTFIX, LEFT_TO_RIGHT },
17         { "++", 3, PREFIX, RIGHT_TO_LEFT },
18         { "--", 3, PREFIX, RIGHT_TO_LEFT },
19         { "+", 3, PREFIX, RIGHT_TO_LEFT },
20         { "-", 3, PREFIX, RIGHT_TO_LEFT },
21         { "~", 3, PREFIX, RIGHT_TO_LEFT },
22         { "!", 3, PREFIX, RIGHT_TO_LEFT },
23         { "*", 4, BINARY, LEFT_TO_RIGHT },
24         { "/", 4, BINARY, LEFT_TO_RIGHT },
25         { "%", 4, BINARY, LEFT_TO_RIGHT },
26         { "+", 5, BINARY, LEFT_TO_RIGHT },
27         { "-", 5, BINARY, LEFT_TO_RIGHT },
28         { "<<", 6, BINARY, LEFT_TO_RIGHT },
29         { ">>", 6, BINARY, LEFT_TO_RIGHT },
30         { "<", 7, BINARY, LEFT_TO_RIGHT },
31         { ">", 7, BINARY, LEFT_TO_RIGHT },
32         { "<=", 7, BINARY, LEFT_TO_RIGHT },
33         { ">=", 7, BINARY, LEFT_TO_RIGHT },
34         { "==", 8, BINARY, LEFT_TO_RIGHT },
35         { "!=", 8, BINARY, LEFT_TO_RIGHT },
36         { "&", 9, BINARY, LEFT_TO_RIGHT },
37         { "^", 10, BINARY, LEFT_TO_RIGHT },
38         { "|", 11, BINARY, LEFT_TO_RIGHT },
39         { "&&", 12, BINARY, LEFT_TO_RIGHT },
40         { "^^", 13, BINARY, LEFT_TO_RIGHT },
41         { "||", 14, BINARY, LEFT_TO_RIGHT },
42         { "?", 15, BINARY, RIGHT_TO_LEFT },
43         { ":", 15, BINARY, RIGHT_TO_LEFT },
44         { "=", 16, BINARY, RIGHT_TO_LEFT },
45         { "+=", 16, BINARY, RIGHT_TO_LEFT },
46         { "-=", 16, BINARY, RIGHT_TO_LEFT },
47         { "*=", 16, BINARY, RIGHT_TO_LEFT },
48         { "/=", 16, BINARY, RIGHT_TO_LEFT },
49         { "%=", 16, BINARY, RIGHT_TO_LEFT },
50         { "<<=", 16, BINARY, RIGHT_TO_LEFT },
51         { ">>=", 16, BINARY, RIGHT_TO_LEFT },
52         { "&=", 16, BINARY, RIGHT_TO_LEFT },
53         { "^=", 16, BINARY, RIGHT_TO_LEFT },
54         { "|=", 16, BINARY, RIGHT_TO_LEFT },
55         { ",", 17, BINARY, LEFT_TO_RIGHT },
56         { { 0 }, 18, NO_OPERATOR, LEFT_TO_RIGHT }
57 };
58
59
60 template<typename C>
61 NodeContainer<C>::NodeContainer(const NodeContainer &c):
62         C(c)
63 {
64         for(typename C::iterator i=this->begin(); i!=this->end(); ++i)
65                 *i = (*i)->clone();
66 }
67
68
69 Statement::Statement():
70         source(0),
71         line(1)
72 { }
73
74
75 Block::Block():
76         use_braces(false),
77         anonymous(false),
78         parent(0)
79 { }
80
81 Block::Block(const Block &other):
82         Node(other),
83         body(other.body),
84         use_braces(other.use_braces),
85         anonymous(other.anonymous),
86         parent(0)
87 { }
88
89 void Block::visit(NodeVisitor &visitor)
90 {
91         visitor.visit(*this);
92 }
93
94
95 void Literal::visit(NodeVisitor &visitor)
96 {
97         visitor.visit(*this);
98 }
99
100
101 void ParenthesizedExpression::visit(NodeVisitor &visitor)
102 {
103         visitor.visit(*this);
104 }
105
106
107 VariableReference::VariableReference():
108         declaration(0)
109 { }
110
111 VariableReference::VariableReference(const VariableReference &other):
112         name(other.name),
113         declaration(0)
114 { }
115
116 void VariableReference::visit(NodeVisitor &visitor)
117 {
118         visitor.visit(*this);
119 }
120
121
122 MemberAccess::MemberAccess():
123         declaration(0)
124 { }
125
126 MemberAccess::MemberAccess(const MemberAccess &other):
127         left(other.left),
128         member(other.member),
129         declaration(0)
130 { }
131
132 void MemberAccess::visit(NodeVisitor &visitor)
133 {
134         visitor.visit(*this);
135 }
136
137
138 UnaryExpression::UnaryExpression():
139         prefix(true)
140 { }
141
142 void UnaryExpression::visit(NodeVisitor &visitor)
143 {
144         visitor.visit(*this);
145 }
146
147
148 void BinaryExpression::visit(NodeVisitor &visitor)
149 {
150         visitor.visit(*this);
151 }
152
153
154 Assignment::Assignment():
155         self_referencing(false),
156         target_declaration(0)
157 { }
158
159 Assignment::Assignment(const Assignment &other):
160         self_referencing(other.self_referencing),
161         target_declaration(0)
162 { }
163
164 void Assignment::visit(NodeVisitor &visitor)
165 {
166         visitor.visit(*this);
167 }
168
169
170 FunctionCall::FunctionCall():
171         declaration(0),
172         constructor(false)
173 { }
174
175 FunctionCall::FunctionCall(const FunctionCall &other):
176         name(other.name),
177         declaration(0),
178         constructor(other.constructor),
179         arguments(other.arguments)
180 { }
181
182 void FunctionCall::visit(NodeVisitor &visitor)
183 {
184         visitor.visit(*this);
185 }
186
187
188 void ExpressionStatement::visit(NodeVisitor &visitor)
189 {
190         visitor.visit(*this);
191 }
192
193
194 void Import::visit(NodeVisitor &visitor)
195 {
196         visitor.visit(*this);
197 }
198
199
200 void Precision::visit(NodeVisitor &visitor)
201 {
202         visitor.visit(*this);
203 }
204
205
206 void Layout::visit(NodeVisitor &visitor)
207 {
208         visitor.visit(*this);
209 }
210
211
212 void InterfaceLayout::visit(NodeVisitor &visitor)
213 {
214         visitor.visit(*this);
215 }
216
217
218 StructDeclaration::StructDeclaration()
219 {
220         members.use_braces = true;
221 }
222
223 void StructDeclaration::visit(NodeVisitor &visitor)
224 {
225         visitor.visit(*this);
226 }
227
228
229 VariableDeclaration::VariableDeclaration():
230         constant(false),
231         type_declaration(0),
232         array(false),
233         linked_declaration(0)
234 { }
235
236 VariableDeclaration::VariableDeclaration(const VariableDeclaration &other):
237         constant(other.constant),
238         sampling(other.sampling),
239         interpolation(other.interpolation),
240         interface(other.interface),
241         precision(other.precision),
242         type(other.type),
243         type_declaration(0),
244         name(other.name),
245         array(other.array),
246         array_size(other.array_size),
247         init_expression(other.init_expression),
248         linked_declaration(0),
249         layout(other.layout)
250 { }
251
252 void VariableDeclaration::visit(NodeVisitor &visitor)
253 {
254         visitor.visit(*this);
255 }
256
257
258 InterfaceBlock::InterfaceBlock():
259         array(false)
260 {
261         members.use_braces = true;
262 }
263
264 void InterfaceBlock::visit(NodeVisitor &visitor)
265 {
266         visitor.visit(*this);
267 }
268
269
270 FunctionDeclaration::FunctionDeclaration():
271         definition(0)
272 { }
273
274 FunctionDeclaration::FunctionDeclaration(const FunctionDeclaration &other):
275         return_type(other.return_type),
276         name(other.name),
277         parameters(other.parameters),
278         definition(other.definition==&other ? this : 0),
279         body(other.body)
280 { }
281
282 void FunctionDeclaration::visit(NodeVisitor &visitor)
283 {
284         visitor.visit(*this);
285 }
286
287
288 void Conditional::visit(NodeVisitor &visitor)
289 {
290         visitor.visit(*this);
291 }
292
293
294 void Iteration::visit(NodeVisitor &visitor)
295 {
296         visitor.visit(*this);
297 }
298
299
300 void Passthrough::visit(NodeVisitor &visitor)
301 {
302         visitor.visit(*this);
303 }
304
305
306 void Return::visit(NodeVisitor &visitor)
307 {
308         visitor.visit(*this);
309 }
310
311
312 void Jump::visit(NodeVisitor &visitor)
313 {
314         visitor.visit(*this);
315 }
316
317
318 Stage::Stage(Stage::Type t):
319         type(t),
320         previous(0)
321 { }
322
323 const char *Stage::get_stage_name(Type type)
324 {
325         static const char *names[] = { "shared", "vertex", "geometry", "fragment" };
326         return names[type];
327 }
328
329
330 Module::Module():
331         shared(Stage::SHARED)
332 { }
333
334 } // namespace SL
335 } // namespace GL
336 } // namespace Msp