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