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