]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/syntax.cpp
Recognize swizzles in GLSL
[libs/gl.git] / source / glsl / syntax.cpp
1 #include <msp/core/maputils.h>
2 #include "syntax.h"
3 #include "visitor.h"
4
5 using namespace std;
6
7 namespace Msp {
8 namespace GL {
9 namespace SL {
10
11 const Operator Operator::operators[] =
12 {
13         { "[", 2, BINARY, LEFT_TO_RIGHT },
14         { "(", 2, POSTFIX, LEFT_TO_RIGHT },
15         { ".", 2, BINARY, LEFT_TO_RIGHT },
16         { "++", 2, POSTFIX, LEFT_TO_RIGHT },
17         { "--", 2, POSTFIX, LEFT_TO_RIGHT },
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         { "!", 3, PREFIX, RIGHT_TO_LEFT },
24         { "*", 4, BINARY, ASSOCIATIVE },
25         { "/", 4, BINARY, LEFT_TO_RIGHT },
26         { "%", 4, BINARY, LEFT_TO_RIGHT },
27         { "+", 5, BINARY, ASSOCIATIVE },
28         { "-", 5, BINARY, LEFT_TO_RIGHT },
29         { "<<", 6, BINARY, LEFT_TO_RIGHT },
30         { ">>", 6, BINARY, LEFT_TO_RIGHT },
31         { "<", 7, BINARY, LEFT_TO_RIGHT },
32         { ">", 7, BINARY, LEFT_TO_RIGHT },
33         { "<=", 7, BINARY, LEFT_TO_RIGHT },
34         { ">=", 7, BINARY, LEFT_TO_RIGHT },
35         { "==", 8, BINARY, LEFT_TO_RIGHT },
36         { "!=", 8, BINARY, LEFT_TO_RIGHT },
37         { "&", 9, BINARY, ASSOCIATIVE },
38         { "^", 10, BINARY, ASSOCIATIVE },
39         { "|", 11, BINARY, ASSOCIATIVE },
40         { "&&", 12, BINARY, ASSOCIATIVE },
41         { "^^", 13, BINARY, ASSOCIATIVE },
42         { "||", 14, BINARY, ASSOCIATIVE },
43         { "?", 15, BINARY, RIGHT_TO_LEFT },
44         { ":", 15, 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         { "|=", 16, BINARY, RIGHT_TO_LEFT },
56         { ",", 17, BINARY, LEFT_TO_RIGHT },
57         { { 0 }, 18, NO_OPERATOR, LEFT_TO_RIGHT }
58 };
59
60 const Operator &Operator::get_operator(const string &token, Type type)
61 {
62         for(const Operator *i=operators; i->type; ++i)
63                 if(i->type==type && i->token==token)
64                         return *i;
65         throw key_error(token);
66 }
67
68
69 template<typename C>
70 NodeContainer<C>::NodeContainer(const NodeContainer &c):
71         C(c)
72 {
73         for(typename C::iterator i=this->begin(); i!=this->end(); ++i)
74                 *i = (*i)->clone();
75 }
76
77
78 Block::Block():
79         use_braces(false),
80         parent(0)
81 { }
82
83 Block::Block(const Block &other):
84         Node(other),
85         body(other.body),
86         use_braces(other.use_braces),
87         parent(0)
88 { }
89
90 void Block::visit(NodeVisitor &visitor)
91 {
92         visitor.visit(*this);
93 }
94
95
96 Expression::Expression():
97         oper(0),
98         type(0),
99         lvalue(false)
100 { }
101
102
103 void Literal::visit(NodeVisitor &visitor)
104 {
105         visitor.visit(*this);
106 }
107
108
109 void ParenthesizedExpression::visit(NodeVisitor &visitor)
110 {
111         visitor.visit(*this);
112 }
113
114
115 VariableReference::VariableReference():
116         declaration(0)
117 { }
118
119 VariableReference::VariableReference(const VariableReference &other):
120         Expression(other),
121         name(other.name),
122         declaration(0)
123 { }
124
125 void VariableReference::visit(NodeVisitor &visitor)
126 {
127         visitor.visit(*this);
128 }
129
130
131 InterfaceBlockReference::InterfaceBlockReference():
132         declaration(0)
133 { }
134
135 InterfaceBlockReference::InterfaceBlockReference(const InterfaceBlockReference &other):
136         Expression(other),
137         name(other.name),
138         declaration(0)
139 { }
140
141 void InterfaceBlockReference::visit(NodeVisitor &visitor)
142 {
143         visitor.visit(*this);
144 }
145
146
147 MemberAccess::MemberAccess():
148         declaration(0)
149 { }
150
151 MemberAccess::MemberAccess(const MemberAccess &other):
152         Expression(other),
153         left(other.left),
154         member(other.member),
155         declaration(0)
156 { }
157
158 void MemberAccess::visit(NodeVisitor &visitor)
159 {
160         visitor.visit(*this);
161 }
162
163
164 Swizzle::Swizzle():
165         count(0)
166 {
167         fill(components, components+4, 0);
168 }
169
170 void Swizzle::visit(NodeVisitor &visitor)
171 {
172         visitor.visit(*this);
173 }
174
175
176 void UnaryExpression::visit(NodeVisitor &visitor)
177 {
178         visitor.visit(*this);
179 }
180
181
182 void BinaryExpression::visit(NodeVisitor &visitor)
183 {
184         visitor.visit(*this);
185 }
186
187
188 Assignment::Assignment():
189         self_referencing(false),
190         target_declaration(0)
191 { }
192
193 Assignment::Assignment(const Assignment &other):
194         BinaryExpression(other),
195         self_referencing(other.self_referencing),
196         target_declaration(0)
197 { }
198
199 void Assignment::visit(NodeVisitor &visitor)
200 {
201         visitor.visit(*this);
202 }
203
204
205 FunctionCall::FunctionCall():
206         constructor(false),
207         declaration(0)
208 { }
209
210 FunctionCall::FunctionCall(const FunctionCall &other):
211         Expression(other),
212         name(other.name),
213         constructor(other.constructor),
214         arguments(other.arguments),
215         declaration(0)
216 { }
217
218 void FunctionCall::visit(NodeVisitor &visitor)
219 {
220         visitor.visit(*this);
221 }
222
223
224 void ExpressionStatement::visit(NodeVisitor &visitor)
225 {
226         visitor.visit(*this);
227 }
228
229
230 void Import::visit(NodeVisitor &visitor)
231 {
232         visitor.visit(*this);
233 }
234
235
236 void Precision::visit(NodeVisitor &visitor)
237 {
238         visitor.visit(*this);
239 }
240
241
242 void Layout::visit(NodeVisitor &visitor)
243 {
244         visitor.visit(*this);
245 }
246
247
248 void InterfaceLayout::visit(NodeVisitor &visitor)
249 {
250         visitor.visit(*this);
251 }
252
253
254 BasicTypeDeclaration::BasicTypeDeclaration():
255         kind(ALIAS),
256         size(0),
257         base_type(0)
258 { }
259
260 BasicTypeDeclaration::BasicTypeDeclaration(const BasicTypeDeclaration &other):
261         TypeDeclaration(other),
262         kind(other.kind),
263         size(other.size),
264         base(other.base),
265         base_type(0)
266 { }
267
268 void BasicTypeDeclaration::visit(NodeVisitor &visitor)
269 {
270         visitor.visit(*this);
271 }
272
273
274 ImageTypeDeclaration::ImageTypeDeclaration():
275         dimensions(TWO),
276         array(false),
277         sampled(true),
278         shadow(false)
279 { }
280
281 void ImageTypeDeclaration::visit(NodeVisitor &visitor)
282 {
283         visitor.visit(*this);
284 }
285
286
287 StructDeclaration::StructDeclaration():
288         interface_block(0)
289 {
290         members.use_braces = true;
291 }
292
293 StructDeclaration::StructDeclaration(const StructDeclaration &other):
294         TypeDeclaration(other),
295         members(other.members),
296         interface_block(0)
297 { }
298
299 StructDeclaration::~StructDeclaration()
300 {
301         if(interface_block && interface_block->struct_declaration==this)
302                 interface_block->struct_declaration = 0;
303 }
304
305 void StructDeclaration::visit(NodeVisitor &visitor)
306 {
307         visitor.visit(*this);
308 }
309
310
311 VariableDeclaration::VariableDeclaration():
312         constant(false),
313         array(false),
314         type_declaration(0),
315         linked_declaration(0)
316 { }
317
318 VariableDeclaration::VariableDeclaration(const VariableDeclaration &other):
319         Statement(other),
320         layout(other.layout),
321         constant(other.constant),
322         sampling(other.sampling),
323         interpolation(other.interpolation),
324         interface(other.interface),
325         precision(other.precision),
326         type(other.type),
327         name(other.name),
328         array(other.array),
329         array_size(other.array_size),
330         init_expression(other.init_expression),
331         type_declaration(0),
332         linked_declaration(0)
333 { }
334
335 VariableDeclaration::~VariableDeclaration()
336 {
337         if(linked_declaration && linked_declaration->linked_declaration==this)
338                 linked_declaration->linked_declaration = 0;
339 }
340
341 void VariableDeclaration::visit(NodeVisitor &visitor)
342 {
343         visitor.visit(*this);
344 }
345
346
347 InterfaceBlock::InterfaceBlock():
348         array(false),
349         type_declaration(0),
350         struct_declaration(0),
351         linked_block(0)
352 { }
353
354 InterfaceBlock::InterfaceBlock(const InterfaceBlock &other):
355         Statement(other),
356         interface(other.interface),
357         name(other.name),
358         members(other.members),
359         instance_name(other.instance_name),
360         array(other.array),
361         type_declaration(0),
362         struct_declaration(0),
363         linked_block(0)
364 { }
365
366 InterfaceBlock::~InterfaceBlock()
367 {
368         if(linked_block && linked_block->linked_block==this)
369                 linked_block->linked_block = 0;
370         if(struct_declaration && struct_declaration->interface_block==this)
371                 struct_declaration->interface_block = 0;
372 }
373
374 void InterfaceBlock::visit(NodeVisitor &visitor)
375 {
376         visitor.visit(*this);
377 }
378
379
380 FunctionDeclaration::FunctionDeclaration():
381         definition(0),
382         return_type_declaration(0)
383 { }
384
385 FunctionDeclaration::FunctionDeclaration(const FunctionDeclaration &other):
386         Statement(other),
387         return_type(other.return_type),
388         name(other.name),
389         parameters(other.parameters),
390         body(other.body),
391         definition(other.definition==&other ? this : 0),
392         return_type_declaration(0)
393 { }
394
395 void FunctionDeclaration::visit(NodeVisitor &visitor)
396 {
397         visitor.visit(*this);
398 }
399
400
401 void Conditional::visit(NodeVisitor &visitor)
402 {
403         visitor.visit(*this);
404 }
405
406
407 void Iteration::visit(NodeVisitor &visitor)
408 {
409         visitor.visit(*this);
410 }
411
412
413 void Passthrough::visit(NodeVisitor &visitor)
414 {
415         visitor.visit(*this);
416 }
417
418
419 void Return::visit(NodeVisitor &visitor)
420 {
421         visitor.visit(*this);
422 }
423
424
425 void Jump::visit(NodeVisitor &visitor)
426 {
427         visitor.visit(*this);
428 }
429
430
431 Stage::Stage(Stage::Type t):
432         type(t),
433         previous(0)
434 { }
435
436 const char *Stage::get_stage_name(Type type)
437 {
438         static const char *names[] = { "shared", "vertex", "geometry", "fragment" };
439         return names[type];
440 }
441
442
443 Module::Module():
444         shared(Stage::SHARED)
445 { }
446
447 } // namespace SL
448 } // namespace GL
449 } // namespace Msp