]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/syntax.cpp
Redesign framebuffer attachment management
[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 VariableReference::VariableReference():
109         declaration(0)
110 { }
111
112 VariableReference::VariableReference(const VariableReference &other):
113         Expression(other),
114         name(other.name),
115         declaration(0)
116 { }
117
118 void VariableReference::visit(NodeVisitor &visitor)
119 {
120         visitor.visit(*this);
121 }
122
123
124 InterfaceBlockReference::InterfaceBlockReference():
125         declaration(0)
126 { }
127
128 InterfaceBlockReference::InterfaceBlockReference(const InterfaceBlockReference &other):
129         Expression(other),
130         name(other.name),
131         declaration(0)
132 { }
133
134 void InterfaceBlockReference::visit(NodeVisitor &visitor)
135 {
136         visitor.visit(*this);
137 }
138
139
140 MemberAccess::MemberAccess():
141         declaration(0),
142         index(-1)
143 { }
144
145 MemberAccess::MemberAccess(const MemberAccess &other):
146         Expression(other),
147         left(other.left),
148         member(other.member),
149         declaration(0),
150         index(-1)
151 { }
152
153 void MemberAccess::visit(NodeVisitor &visitor)
154 {
155         visitor.visit(*this);
156 }
157
158
159 Swizzle::Swizzle():
160         count(0)
161 {
162         fill(components, components+4, 0);
163 }
164
165 void Swizzle::visit(NodeVisitor &visitor)
166 {
167         visitor.visit(*this);
168 }
169
170
171 void UnaryExpression::visit(NodeVisitor &visitor)
172 {
173         visitor.visit(*this);
174 }
175
176
177 void BinaryExpression::visit(NodeVisitor &visitor)
178 {
179         visitor.visit(*this);
180 }
181
182
183 Assignment::Assignment():
184         self_referencing(false)
185 { }
186
187 Assignment::Assignment(const Assignment &other):
188         BinaryExpression(other),
189         self_referencing(other.self_referencing)
190 { }
191
192 void Assignment::visit(NodeVisitor &visitor)
193 {
194         visitor.visit(*this);
195 }
196
197
198 Assignment::Target::Target(Statement *d):
199         declaration(d),
200         chain_len(0)
201 {
202         fill(chain, chain+7, 0);
203 }
204
205 bool Assignment::Target::operator<(const Target &other) const
206 {
207         if(declaration!=other.declaration)
208                 return declaration<other.declaration;
209         for(unsigned i=0; (i<7 && i<chain_len && i<other.chain_len); ++i)
210                 if(chain[i]!=other.chain[i])
211                         return chain[i]<other.chain[i];
212         return chain_len<other.chain_len;
213 }
214
215
216 void TernaryExpression::visit(NodeVisitor &visitor)
217 {
218         visitor.visit(*this);
219 }
220
221
222 FunctionCall::FunctionCall():
223         constructor(false),
224         declaration(0)
225 { }
226
227 FunctionCall::FunctionCall(const FunctionCall &other):
228         Expression(other),
229         name(other.name),
230         constructor(other.constructor),
231         arguments(other.arguments),
232         declaration(0)
233 { }
234
235 void FunctionCall::visit(NodeVisitor &visitor)
236 {
237         visitor.visit(*this);
238 }
239
240
241 void ExpressionStatement::visit(NodeVisitor &visitor)
242 {
243         visitor.visit(*this);
244 }
245
246
247 void Import::visit(NodeVisitor &visitor)
248 {
249         visitor.visit(*this);
250 }
251
252
253 void Precision::visit(NodeVisitor &visitor)
254 {
255         visitor.visit(*this);
256 }
257
258
259 void Layout::visit(NodeVisitor &visitor)
260 {
261         visitor.visit(*this);
262 }
263
264
265 void InterfaceLayout::visit(NodeVisitor &visitor)
266 {
267         visitor.visit(*this);
268 }
269
270
271 BasicTypeDeclaration::BasicTypeDeclaration():
272         kind(ALIAS),
273         size(0),
274         sign(true),
275         base_type(0)
276 { }
277
278 BasicTypeDeclaration::BasicTypeDeclaration(const BasicTypeDeclaration &other):
279         TypeDeclaration(other),
280         kind(other.kind),
281         size(other.size),
282         sign(other.sign),
283         base(other.base),
284         base_type(0)
285 { }
286
287 void BasicTypeDeclaration::visit(NodeVisitor &visitor)
288 {
289         visitor.visit(*this);
290 }
291
292
293 ImageTypeDeclaration::ImageTypeDeclaration():
294         dimensions(TWO),
295         array(false),
296         sampled(true),
297         shadow(false)
298 { }
299
300 void ImageTypeDeclaration::visit(NodeVisitor &visitor)
301 {
302         visitor.visit(*this);
303 }
304
305
306 StructDeclaration::StructDeclaration():
307         interface_block(0)
308 {
309         members.use_braces = true;
310 }
311
312 StructDeclaration::StructDeclaration(const StructDeclaration &other):
313         TypeDeclaration(other),
314         members(other.members),
315         interface_block(0)
316 { }
317
318 StructDeclaration::~StructDeclaration()
319 {
320         if(interface_block && interface_block->struct_declaration==this)
321                 interface_block->struct_declaration = 0;
322 }
323
324 void StructDeclaration::visit(NodeVisitor &visitor)
325 {
326         visitor.visit(*this);
327 }
328
329
330 VariableDeclaration::VariableDeclaration():
331         constant(false),
332         array(false),
333         type_declaration(0),
334         linked_declaration(0)
335 { }
336
337 VariableDeclaration::VariableDeclaration(const VariableDeclaration &other):
338         Statement(other),
339         layout(other.layout),
340         constant(other.constant),
341         sampling(other.sampling),
342         interpolation(other.interpolation),
343         interface(other.interface),
344         precision(other.precision),
345         type(other.type),
346         name(other.name),
347         array(other.array),
348         array_size(other.array_size),
349         init_expression(other.init_expression),
350         type_declaration(0),
351         linked_declaration(0)
352 { }
353
354 VariableDeclaration::~VariableDeclaration()
355 {
356         if(linked_declaration && linked_declaration->linked_declaration==this)
357                 linked_declaration->linked_declaration = 0;
358 }
359
360 void VariableDeclaration::visit(NodeVisitor &visitor)
361 {
362         visitor.visit(*this);
363 }
364
365
366 InterfaceBlock::InterfaceBlock():
367         array(false),
368         type_declaration(0),
369         struct_declaration(0),
370         linked_block(0)
371 { }
372
373 InterfaceBlock::InterfaceBlock(const InterfaceBlock &other):
374         Statement(other),
375         interface(other.interface),
376         block_name(other.block_name),
377         members(other.members),
378         instance_name(other.instance_name),
379         array(other.array),
380         type_declaration(0),
381         struct_declaration(0),
382         linked_block(0)
383 { }
384
385 InterfaceBlock::~InterfaceBlock()
386 {
387         if(linked_block && linked_block->linked_block==this)
388                 linked_block->linked_block = 0;
389         if(struct_declaration && struct_declaration->interface_block==this)
390                 struct_declaration->interface_block = 0;
391 }
392
393 void InterfaceBlock::visit(NodeVisitor &visitor)
394 {
395         visitor.visit(*this);
396 }
397
398
399 FunctionDeclaration::FunctionDeclaration():
400         virtua(false),
401         overrd(false),
402         definition(0),
403         return_type_declaration(0)
404 { }
405
406 FunctionDeclaration::FunctionDeclaration(const FunctionDeclaration &other):
407         Statement(other),
408         return_type(other.return_type),
409         name(other.name),
410         parameters(other.parameters),
411         virtua(other.virtua),
412         overrd(other.overrd),
413         body(other.body),
414         signature(other.signature),
415         definition(other.definition==&other ? this : 0),
416         return_type_declaration(0)
417 { }
418
419 void FunctionDeclaration::visit(NodeVisitor &visitor)
420 {
421         visitor.visit(*this);
422 }
423
424
425 void Conditional::visit(NodeVisitor &visitor)
426 {
427         visitor.visit(*this);
428 }
429
430
431 void Iteration::visit(NodeVisitor &visitor)
432 {
433         visitor.visit(*this);
434 }
435
436
437 void Passthrough::visit(NodeVisitor &visitor)
438 {
439         visitor.visit(*this);
440 }
441
442
443 void Return::visit(NodeVisitor &visitor)
444 {
445         visitor.visit(*this);
446 }
447
448
449 void Jump::visit(NodeVisitor &visitor)
450 {
451         visitor.visit(*this);
452 }
453
454
455 Stage::Stage(Stage::Type t):
456         type(t),
457         previous(0)
458 { }
459
460 const char *Stage::get_stage_name(Type type)
461 {
462         static const char *names[] = { "shared", "vertex", "geometry", "fragment" };
463         return names[type];
464 }
465
466
467 Module::Module():
468         shared(Stage::SHARED)
469 { }
470
471
472 string get_unused_variable_name(const Block &block, const string &base)
473 {
474         string name = base;
475
476         unsigned number = 1;
477         unsigned base_size = name.size();
478         while(1)
479         {
480                 bool unused = true;
481                 for(const Block *b=&block; (unused && b); b=b->parent)
482                         unused = !b->variables.count(name);
483                 if(unused)
484                         return name;
485
486                 name.erase(base_size);
487                 name += format("_%d", number);
488                 ++number;
489         }
490 }
491
492 int get_layout_value(const Layout &layout, const string &name, int def_value)
493 {
494         for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); ++i)
495                 if(i->name==name)
496                         return i->value;
497         return def_value;
498 }
499
500 void add_to_chain(Assignment::Target &target, Assignment::Target::ChainType type, unsigned index)
501 {
502         if(target.chain_len<7)
503                 target.chain[target.chain_len] = type | min<unsigned>(index, 0x3F);
504         ++target.chain_len;
505 }
506
507 } // namespace SL
508 } // namespace GL
509 } // namespace Msp