]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/syntax.cpp
Assign a result type to all expressions
[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 {
277         members.use_braces = true;
278 }
279
280 void StructDeclaration::visit(NodeVisitor &visitor)
281 {
282         visitor.visit(*this);
283 }
284
285
286 VariableDeclaration::VariableDeclaration():
287         constant(false),
288         array(false),
289         type_declaration(0),
290         linked_declaration(0)
291 { }
292
293 VariableDeclaration::VariableDeclaration(const VariableDeclaration &other):
294         Statement(other),
295         layout(other.layout),
296         constant(other.constant),
297         sampling(other.sampling),
298         interpolation(other.interpolation),
299         interface(other.interface),
300         precision(other.precision),
301         type(other.type),
302         name(other.name),
303         array(other.array),
304         array_size(other.array_size),
305         init_expression(other.init_expression),
306         type_declaration(0),
307         linked_declaration(0)
308 { }
309
310 VariableDeclaration::~VariableDeclaration()
311 {
312         if(linked_declaration && linked_declaration->linked_declaration==this)
313                 linked_declaration->linked_declaration = 0;
314 }
315
316 void VariableDeclaration::visit(NodeVisitor &visitor)
317 {
318         visitor.visit(*this);
319 }
320
321
322 InterfaceBlock::InterfaceBlock():
323         array(false),
324         linked_block(0)
325 {
326         members.use_braces = true;
327 }
328
329 InterfaceBlock::InterfaceBlock(const InterfaceBlock &other):
330         Statement(other),
331         interface(other.interface),
332         name(other.name),
333         members(other.members),
334         instance_name(other.instance_name),
335         array(other.array),
336         linked_block(0)
337 { }
338
339 InterfaceBlock::~InterfaceBlock()
340 {
341         if(linked_block && linked_block->linked_block==this)
342                 linked_block->linked_block = 0;
343 }
344
345 void InterfaceBlock::visit(NodeVisitor &visitor)
346 {
347         visitor.visit(*this);
348 }
349
350
351 FunctionDeclaration::FunctionDeclaration():
352         definition(0),
353         return_type_declaration(0)
354 { }
355
356 FunctionDeclaration::FunctionDeclaration(const FunctionDeclaration &other):
357         Statement(other),
358         return_type(other.return_type),
359         name(other.name),
360         parameters(other.parameters),
361         body(other.body),
362         definition(other.definition==&other ? this : 0),
363         return_type_declaration(0)
364 { }
365
366 void FunctionDeclaration::visit(NodeVisitor &visitor)
367 {
368         visitor.visit(*this);
369 }
370
371
372 void Conditional::visit(NodeVisitor &visitor)
373 {
374         visitor.visit(*this);
375 }
376
377
378 void Iteration::visit(NodeVisitor &visitor)
379 {
380         visitor.visit(*this);
381 }
382
383
384 void Passthrough::visit(NodeVisitor &visitor)
385 {
386         visitor.visit(*this);
387 }
388
389
390 void Return::visit(NodeVisitor &visitor)
391 {
392         visitor.visit(*this);
393 }
394
395
396 void Jump::visit(NodeVisitor &visitor)
397 {
398         visitor.visit(*this);
399 }
400
401
402 Stage::Stage(Stage::Type t):
403         type(t),
404         previous(0)
405 { }
406
407 const char *Stage::get_stage_name(Type type)
408 {
409         static const char *names[] = { "shared", "vertex", "geometry", "fragment" };
410         return names[type];
411 }
412
413
414 Module::Module():
415         shared(Stage::SHARED)
416 { }
417
418 } // namespace SL
419 } // namespace GL
420 } // namespace Msp