]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/syntax.cpp
Resolve the return types of 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, 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 { }
99
100
101 void Literal::visit(NodeVisitor &visitor)
102 {
103         visitor.visit(*this);
104 }
105
106
107 void ParenthesizedExpression::visit(NodeVisitor &visitor)
108 {
109         visitor.visit(*this);
110 }
111
112
113 VariableReference::VariableReference():
114         declaration(0)
115 { }
116
117 VariableReference::VariableReference(const VariableReference &other):
118         Expression(other),
119         name(other.name),
120         declaration(0)
121 { }
122
123 void VariableReference::visit(NodeVisitor &visitor)
124 {
125         visitor.visit(*this);
126 }
127
128
129 InterfaceBlockReference::InterfaceBlockReference():
130         declaration(0)
131 { }
132
133 InterfaceBlockReference::InterfaceBlockReference(const InterfaceBlockReference &other):
134         Expression(other),
135         name(other.name),
136         declaration(0)
137 { }
138
139 void InterfaceBlockReference::visit(NodeVisitor &visitor)
140 {
141         visitor.visit(*this);
142 }
143
144
145 MemberAccess::MemberAccess():
146         declaration(0)
147 { }
148
149 MemberAccess::MemberAccess(const MemberAccess &other):
150         Expression(other),
151         left(other.left),
152         member(other.member),
153         declaration(0)
154 { }
155
156 void MemberAccess::visit(NodeVisitor &visitor)
157 {
158         visitor.visit(*this);
159 }
160
161
162 void UnaryExpression::visit(NodeVisitor &visitor)
163 {
164         visitor.visit(*this);
165 }
166
167
168 void BinaryExpression::visit(NodeVisitor &visitor)
169 {
170         visitor.visit(*this);
171 }
172
173
174 Assignment::Assignment():
175         self_referencing(false),
176         target_declaration(0)
177 { }
178
179 Assignment::Assignment(const Assignment &other):
180         BinaryExpression(other),
181         self_referencing(other.self_referencing),
182         target_declaration(0)
183 { }
184
185 void Assignment::visit(NodeVisitor &visitor)
186 {
187         visitor.visit(*this);
188 }
189
190
191 FunctionCall::FunctionCall():
192         constructor(false),
193         declaration(0)
194 { }
195
196 FunctionCall::FunctionCall(const FunctionCall &other):
197         Expression(other),
198         name(other.name),
199         constructor(other.constructor),
200         arguments(other.arguments),
201         declaration(0)
202 { }
203
204 void FunctionCall::visit(NodeVisitor &visitor)
205 {
206         visitor.visit(*this);
207 }
208
209
210 void ExpressionStatement::visit(NodeVisitor &visitor)
211 {
212         visitor.visit(*this);
213 }
214
215
216 void Import::visit(NodeVisitor &visitor)
217 {
218         visitor.visit(*this);
219 }
220
221
222 void Precision::visit(NodeVisitor &visitor)
223 {
224         visitor.visit(*this);
225 }
226
227
228 void Layout::visit(NodeVisitor &visitor)
229 {
230         visitor.visit(*this);
231 }
232
233
234 void InterfaceLayout::visit(NodeVisitor &visitor)
235 {
236         visitor.visit(*this);
237 }
238
239
240 BasicTypeDeclaration::BasicTypeDeclaration():
241         kind(ALIAS),
242         size(0),
243         base_type(0)
244 { }
245
246 BasicTypeDeclaration::BasicTypeDeclaration(const BasicTypeDeclaration &other):
247         TypeDeclaration(other),
248         kind(other.kind),
249         size(other.size),
250         base(other.base),
251         base_type(0)
252 { }
253
254 void BasicTypeDeclaration::visit(NodeVisitor &visitor)
255 {
256         visitor.visit(*this);
257 }
258
259
260 ImageTypeDeclaration::ImageTypeDeclaration():
261         dimensions(TWO),
262         array(false),
263         sampled(true),
264         shadow(false)
265 { }
266
267 void ImageTypeDeclaration::visit(NodeVisitor &visitor)
268 {
269         visitor.visit(*this);
270 }
271
272
273 StructDeclaration::StructDeclaration()
274 {
275         members.use_braces = true;
276 }
277
278 void StructDeclaration::visit(NodeVisitor &visitor)
279 {
280         visitor.visit(*this);
281 }
282
283
284 VariableDeclaration::VariableDeclaration():
285         constant(false),
286         array(false),
287         type_declaration(0),
288         linked_declaration(0)
289 { }
290
291 VariableDeclaration::VariableDeclaration(const VariableDeclaration &other):
292         Statement(other),
293         layout(other.layout),
294         constant(other.constant),
295         sampling(other.sampling),
296         interpolation(other.interpolation),
297         interface(other.interface),
298         precision(other.precision),
299         type(other.type),
300         name(other.name),
301         array(other.array),
302         array_size(other.array_size),
303         init_expression(other.init_expression),
304         type_declaration(0),
305         linked_declaration(0)
306 { }
307
308 VariableDeclaration::~VariableDeclaration()
309 {
310         if(linked_declaration && linked_declaration->linked_declaration==this)
311                 linked_declaration->linked_declaration = 0;
312 }
313
314 void VariableDeclaration::visit(NodeVisitor &visitor)
315 {
316         visitor.visit(*this);
317 }
318
319
320 InterfaceBlock::InterfaceBlock():
321         array(false),
322         linked_block(0)
323 {
324         members.use_braces = true;
325 }
326
327 InterfaceBlock::InterfaceBlock(const InterfaceBlock &other):
328         Statement(other),
329         interface(other.interface),
330         name(other.name),
331         members(other.members),
332         instance_name(other.instance_name),
333         array(other.array),
334         linked_block(0)
335 { }
336
337 InterfaceBlock::~InterfaceBlock()
338 {
339         if(linked_block && linked_block->linked_block==this)
340                 linked_block->linked_block = 0;
341 }
342
343 void InterfaceBlock::visit(NodeVisitor &visitor)
344 {
345         visitor.visit(*this);
346 }
347
348
349 FunctionDeclaration::FunctionDeclaration():
350         definition(0),
351         return_type_declaration(0)
352 { }
353
354 FunctionDeclaration::FunctionDeclaration(const FunctionDeclaration &other):
355         Statement(other),
356         return_type(other.return_type),
357         name(other.name),
358         parameters(other.parameters),
359         body(other.body),
360         definition(other.definition==&other ? this : 0),
361         return_type_declaration(0)
362 { }
363
364 void FunctionDeclaration::visit(NodeVisitor &visitor)
365 {
366         visitor.visit(*this);
367 }
368
369
370 void Conditional::visit(NodeVisitor &visitor)
371 {
372         visitor.visit(*this);
373 }
374
375
376 void Iteration::visit(NodeVisitor &visitor)
377 {
378         visitor.visit(*this);
379 }
380
381
382 void Passthrough::visit(NodeVisitor &visitor)
383 {
384         visitor.visit(*this);
385 }
386
387
388 void Return::visit(NodeVisitor &visitor)
389 {
390         visitor.visit(*this);
391 }
392
393
394 void Jump::visit(NodeVisitor &visitor)
395 {
396         visitor.visit(*this);
397 }
398
399
400 Stage::Stage(Stage::Type t):
401         type(t),
402         previous(0)
403 { }
404
405 const char *Stage::get_stage_name(Type type)
406 {
407         static const char *names[] = { "shared", "vertex", "geometry", "fragment" };
408         return names[type];
409 }
410
411
412 Module::Module():
413         shared(Stage::SHARED)
414 { }
415
416 } // namespace SL
417 } // namespace GL
418 } // namespace Msp