]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/syntax.cpp
Transform interface block contents into structs
[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 void UnaryExpression::visit(NodeVisitor &visitor)
165 {
166         visitor.visit(*this);
167 }
168
169
170 void BinaryExpression::visit(NodeVisitor &visitor)
171 {
172         visitor.visit(*this);
173 }
174
175
176 Assignment::Assignment():
177         self_referencing(false),
178         target_declaration(0)
179 { }
180
181 Assignment::Assignment(const Assignment &other):
182         BinaryExpression(other),
183         self_referencing(other.self_referencing),
184         target_declaration(0)
185 { }
186
187 void Assignment::visit(NodeVisitor &visitor)
188 {
189         visitor.visit(*this);
190 }
191
192
193 FunctionCall::FunctionCall():
194         constructor(false),
195         declaration(0)
196 { }
197
198 FunctionCall::FunctionCall(const FunctionCall &other):
199         Expression(other),
200         name(other.name),
201         constructor(other.constructor),
202         arguments(other.arguments),
203         declaration(0)
204 { }
205
206 void FunctionCall::visit(NodeVisitor &visitor)
207 {
208         visitor.visit(*this);
209 }
210
211
212 void ExpressionStatement::visit(NodeVisitor &visitor)
213 {
214         visitor.visit(*this);
215 }
216
217
218 void Import::visit(NodeVisitor &visitor)
219 {
220         visitor.visit(*this);
221 }
222
223
224 void Precision::visit(NodeVisitor &visitor)
225 {
226         visitor.visit(*this);
227 }
228
229
230 void Layout::visit(NodeVisitor &visitor)
231 {
232         visitor.visit(*this);
233 }
234
235
236 void InterfaceLayout::visit(NodeVisitor &visitor)
237 {
238         visitor.visit(*this);
239 }
240
241
242 BasicTypeDeclaration::BasicTypeDeclaration():
243         kind(ALIAS),
244         size(0),
245         base_type(0)
246 { }
247
248 BasicTypeDeclaration::BasicTypeDeclaration(const BasicTypeDeclaration &other):
249         TypeDeclaration(other),
250         kind(other.kind),
251         size(other.size),
252         base(other.base),
253         base_type(0)
254 { }
255
256 void BasicTypeDeclaration::visit(NodeVisitor &visitor)
257 {
258         visitor.visit(*this);
259 }
260
261
262 ImageTypeDeclaration::ImageTypeDeclaration():
263         dimensions(TWO),
264         array(false),
265         sampled(true),
266         shadow(false)
267 { }
268
269 void ImageTypeDeclaration::visit(NodeVisitor &visitor)
270 {
271         visitor.visit(*this);
272 }
273
274
275 StructDeclaration::StructDeclaration():
276         interface_block(0)
277 {
278         members.use_braces = true;
279 }
280
281 StructDeclaration::StructDeclaration(const StructDeclaration &other):
282         TypeDeclaration(other),
283         members(other.members),
284         interface_block(0)
285 { }
286
287 StructDeclaration::~StructDeclaration()
288 {
289         if(interface_block && interface_block->struct_declaration==this)
290                 interface_block->struct_declaration = 0;
291 }
292
293 void StructDeclaration::visit(NodeVisitor &visitor)
294 {
295         visitor.visit(*this);
296 }
297
298
299 VariableDeclaration::VariableDeclaration():
300         constant(false),
301         array(false),
302         type_declaration(0),
303         linked_declaration(0)
304 { }
305
306 VariableDeclaration::VariableDeclaration(const VariableDeclaration &other):
307         Statement(other),
308         layout(other.layout),
309         constant(other.constant),
310         sampling(other.sampling),
311         interpolation(other.interpolation),
312         interface(other.interface),
313         precision(other.precision),
314         type(other.type),
315         name(other.name),
316         array(other.array),
317         array_size(other.array_size),
318         init_expression(other.init_expression),
319         type_declaration(0),
320         linked_declaration(0)
321 { }
322
323 VariableDeclaration::~VariableDeclaration()
324 {
325         if(linked_declaration && linked_declaration->linked_declaration==this)
326                 linked_declaration->linked_declaration = 0;
327 }
328
329 void VariableDeclaration::visit(NodeVisitor &visitor)
330 {
331         visitor.visit(*this);
332 }
333
334
335 InterfaceBlock::InterfaceBlock():
336         array(false),
337         type_declaration(0),
338         struct_declaration(0),
339         linked_block(0)
340 { }
341
342 InterfaceBlock::InterfaceBlock(const InterfaceBlock &other):
343         Statement(other),
344         interface(other.interface),
345         name(other.name),
346         members(other.members),
347         instance_name(other.instance_name),
348         array(other.array),
349         type_declaration(0),
350         struct_declaration(0),
351         linked_block(0)
352 { }
353
354 InterfaceBlock::~InterfaceBlock()
355 {
356         if(linked_block && linked_block->linked_block==this)
357                 linked_block->linked_block = 0;
358         if(struct_declaration && struct_declaration->interface_block==this)
359                 struct_declaration->interface_block = 0;
360 }
361
362 void InterfaceBlock::visit(NodeVisitor &visitor)
363 {
364         visitor.visit(*this);
365 }
366
367
368 FunctionDeclaration::FunctionDeclaration():
369         definition(0),
370         return_type_declaration(0)
371 { }
372
373 FunctionDeclaration::FunctionDeclaration(const FunctionDeclaration &other):
374         Statement(other),
375         return_type(other.return_type),
376         name(other.name),
377         parameters(other.parameters),
378         body(other.body),
379         definition(other.definition==&other ? this : 0),
380         return_type_declaration(0)
381 { }
382
383 void FunctionDeclaration::visit(NodeVisitor &visitor)
384 {
385         visitor.visit(*this);
386 }
387
388
389 void Conditional::visit(NodeVisitor &visitor)
390 {
391         visitor.visit(*this);
392 }
393
394
395 void Iteration::visit(NodeVisitor &visitor)
396 {
397         visitor.visit(*this);
398 }
399
400
401 void Passthrough::visit(NodeVisitor &visitor)
402 {
403         visitor.visit(*this);
404 }
405
406
407 void Return::visit(NodeVisitor &visitor)
408 {
409         visitor.visit(*this);
410 }
411
412
413 void Jump::visit(NodeVisitor &visitor)
414 {
415         visitor.visit(*this);
416 }
417
418
419 Stage::Stage(Stage::Type t):
420         type(t),
421         previous(0)
422 { }
423
424 const char *Stage::get_stage_name(Type type)
425 {
426         static const char *names[] = { "shared", "vertex", "geometry", "fragment" };
427         return names[type];
428 }
429
430
431 Module::Module():
432         shared(Stage::SHARED)
433 { }
434
435 } // namespace SL
436 } // namespace GL
437 } // namespace Msp