]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/syntax.cpp
Add GLSL keywords for overriding functions
[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, TERNARY, RIGHT_TO_LEFT },
44         { ":", 15, TERNARY, 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 { }
191
192 Assignment::Assignment(const Assignment &other):
193         BinaryExpression(other),
194         self_referencing(other.self_referencing)
195 { }
196
197 void Assignment::visit(NodeVisitor &visitor)
198 {
199         visitor.visit(*this);
200 }
201
202
203 Assignment::Target::Target(Statement *d):
204         declaration(d),
205         chain_len(0)
206 {
207         fill(chain, chain+7, 0);
208 }
209
210 bool Assignment::Target::operator<(const Target &other) const
211 {
212         if(declaration!=other.declaration)
213                 return declaration<other.declaration;
214         for(unsigned i=0; (i<7 && i<chain_len && i<other.chain_len); ++i)
215                 if(chain[i]!=other.chain[i])
216                         return chain[i]<other.chain[i];
217         return chain_len<other.chain_len;
218 }
219
220
221 void TernaryExpression::visit(NodeVisitor &visitor)
222 {
223         visitor.visit(*this);
224 }
225
226
227 FunctionCall::FunctionCall():
228         constructor(false),
229         declaration(0)
230 { }
231
232 FunctionCall::FunctionCall(const FunctionCall &other):
233         Expression(other),
234         name(other.name),
235         constructor(other.constructor),
236         arguments(other.arguments),
237         declaration(0)
238 { }
239
240 void FunctionCall::visit(NodeVisitor &visitor)
241 {
242         visitor.visit(*this);
243 }
244
245
246 void ExpressionStatement::visit(NodeVisitor &visitor)
247 {
248         visitor.visit(*this);
249 }
250
251
252 void Import::visit(NodeVisitor &visitor)
253 {
254         visitor.visit(*this);
255 }
256
257
258 void Precision::visit(NodeVisitor &visitor)
259 {
260         visitor.visit(*this);
261 }
262
263
264 void Layout::visit(NodeVisitor &visitor)
265 {
266         visitor.visit(*this);
267 }
268
269
270 void InterfaceLayout::visit(NodeVisitor &visitor)
271 {
272         visitor.visit(*this);
273 }
274
275
276 BasicTypeDeclaration::BasicTypeDeclaration():
277         kind(ALIAS),
278         size(0),
279         base_type(0)
280 { }
281
282 BasicTypeDeclaration::BasicTypeDeclaration(const BasicTypeDeclaration &other):
283         TypeDeclaration(other),
284         kind(other.kind),
285         size(other.size),
286         base(other.base),
287         base_type(0)
288 { }
289
290 void BasicTypeDeclaration::visit(NodeVisitor &visitor)
291 {
292         visitor.visit(*this);
293 }
294
295
296 ImageTypeDeclaration::ImageTypeDeclaration():
297         dimensions(TWO),
298         array(false),
299         sampled(true),
300         shadow(false)
301 { }
302
303 void ImageTypeDeclaration::visit(NodeVisitor &visitor)
304 {
305         visitor.visit(*this);
306 }
307
308
309 StructDeclaration::StructDeclaration():
310         interface_block(0)
311 {
312         members.use_braces = true;
313 }
314
315 StructDeclaration::StructDeclaration(const StructDeclaration &other):
316         TypeDeclaration(other),
317         members(other.members),
318         interface_block(0)
319 { }
320
321 StructDeclaration::~StructDeclaration()
322 {
323         if(interface_block && interface_block->struct_declaration==this)
324                 interface_block->struct_declaration = 0;
325 }
326
327 void StructDeclaration::visit(NodeVisitor &visitor)
328 {
329         visitor.visit(*this);
330 }
331
332
333 VariableDeclaration::VariableDeclaration():
334         constant(false),
335         array(false),
336         type_declaration(0),
337         linked_declaration(0)
338 { }
339
340 VariableDeclaration::VariableDeclaration(const VariableDeclaration &other):
341         Statement(other),
342         layout(other.layout),
343         constant(other.constant),
344         sampling(other.sampling),
345         interpolation(other.interpolation),
346         interface(other.interface),
347         precision(other.precision),
348         type(other.type),
349         name(other.name),
350         array(other.array),
351         array_size(other.array_size),
352         init_expression(other.init_expression),
353         type_declaration(0),
354         linked_declaration(0)
355 { }
356
357 VariableDeclaration::~VariableDeclaration()
358 {
359         if(linked_declaration && linked_declaration->linked_declaration==this)
360                 linked_declaration->linked_declaration = 0;
361 }
362
363 void VariableDeclaration::visit(NodeVisitor &visitor)
364 {
365         visitor.visit(*this);
366 }
367
368
369 InterfaceBlock::InterfaceBlock():
370         array(false),
371         type_declaration(0),
372         struct_declaration(0),
373         linked_block(0)
374 { }
375
376 InterfaceBlock::InterfaceBlock(const InterfaceBlock &other):
377         Statement(other),
378         interface(other.interface),
379         name(other.name),
380         members(other.members),
381         instance_name(other.instance_name),
382         array(other.array),
383         type_declaration(0),
384         struct_declaration(0),
385         linked_block(0)
386 { }
387
388 InterfaceBlock::~InterfaceBlock()
389 {
390         if(linked_block && linked_block->linked_block==this)
391                 linked_block->linked_block = 0;
392         if(struct_declaration && struct_declaration->interface_block==this)
393                 struct_declaration->interface_block = 0;
394 }
395
396 void InterfaceBlock::visit(NodeVisitor &visitor)
397 {
398         visitor.visit(*this);
399 }
400
401
402 FunctionDeclaration::FunctionDeclaration():
403         virtua(false),
404         overrd(false),
405         definition(0),
406         return_type_declaration(0)
407 { }
408
409 FunctionDeclaration::FunctionDeclaration(const FunctionDeclaration &other):
410         Statement(other),
411         return_type(other.return_type),
412         name(other.name),
413         parameters(other.parameters),
414         virtua(other.virtua),
415         overrd(other.overrd),
416         body(other.body),
417         signature(other.signature),
418         definition(other.definition==&other ? this : 0),
419         return_type_declaration(0)
420 { }
421
422 void FunctionDeclaration::visit(NodeVisitor &visitor)
423 {
424         visitor.visit(*this);
425 }
426
427
428 void Conditional::visit(NodeVisitor &visitor)
429 {
430         visitor.visit(*this);
431 }
432
433
434 void Iteration::visit(NodeVisitor &visitor)
435 {
436         visitor.visit(*this);
437 }
438
439
440 void Passthrough::visit(NodeVisitor &visitor)
441 {
442         visitor.visit(*this);
443 }
444
445
446 void Return::visit(NodeVisitor &visitor)
447 {
448         visitor.visit(*this);
449 }
450
451
452 void Jump::visit(NodeVisitor &visitor)
453 {
454         visitor.visit(*this);
455 }
456
457
458 Stage::Stage(Stage::Type t):
459         type(t),
460         previous(0)
461 { }
462
463 const char *Stage::get_stage_name(Type type)
464 {
465         static const char *names[] = { "shared", "vertex", "geometry", "fragment" };
466         return names[type];
467 }
468
469
470 Module::Module():
471         shared(Stage::SHARED)
472 { }
473
474 } // namespace SL
475 } // namespace GL
476 } // namespace Msp