]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/syntax.cpp
Inject builtins into the module
[libs/gl.git] / source / glsl / syntax.cpp
1 #include "syntax.h"
2 #include "visitor.h"
3
4 using namespace std;
5
6 namespace Msp {
7 namespace GL {
8 namespace SL {
9
10 const Operator Operator::operators[] =
11 {
12         { "[", 2, BINARY, LEFT_TO_RIGHT },
13         { "(", 2, BINARY, LEFT_TO_RIGHT },
14         { ".", 2, BINARY, LEFT_TO_RIGHT },
15         { "++", 2, POSTFIX, LEFT_TO_RIGHT },
16         { "--", 2, POSTFIX, LEFT_TO_RIGHT },
17         { "++", 3, PREFIX, RIGHT_TO_LEFT },
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         { "*", 4, BINARY, LEFT_TO_RIGHT },
24         { "/", 4, BINARY, LEFT_TO_RIGHT },
25         { "%", 4, BINARY, LEFT_TO_RIGHT },
26         { "+", 5, BINARY, LEFT_TO_RIGHT },
27         { "-", 5, BINARY, LEFT_TO_RIGHT },
28         { "<<", 6, BINARY, LEFT_TO_RIGHT },
29         { ">>", 6, BINARY, LEFT_TO_RIGHT },
30         { "<", 7, BINARY, LEFT_TO_RIGHT },
31         { ">", 7, BINARY, LEFT_TO_RIGHT },
32         { "<=", 7, BINARY, LEFT_TO_RIGHT },
33         { ">=", 7, BINARY, LEFT_TO_RIGHT },
34         { "==", 8, BINARY, LEFT_TO_RIGHT },
35         { "!=", 8, BINARY, LEFT_TO_RIGHT },
36         { "&", 9, BINARY, LEFT_TO_RIGHT },
37         { "^", 10, BINARY, LEFT_TO_RIGHT },
38         { "|", 11, BINARY, LEFT_TO_RIGHT },
39         { "&&", 12, BINARY, LEFT_TO_RIGHT },
40         { "^^", 13, BINARY, LEFT_TO_RIGHT },
41         { "||", 14, BINARY, LEFT_TO_RIGHT },
42         { "?", 15, BINARY, RIGHT_TO_LEFT },
43         { ":", 15, BINARY, 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
60 template<typename C>
61 NodeContainer<C>::NodeContainer(const NodeContainer &c):
62         C(c)
63 {
64         for(typename C::iterator i=this->begin(); i!=this->end(); ++i)
65                 *i = (*i)->clone();
66 }
67
68
69 Statement::Statement():
70         source(GENERATED_SOURCE),
71         line(1)
72 { }
73
74
75 Block::Block():
76         use_braces(false),
77         parent(0)
78 { }
79
80 Block::Block(const Block &other):
81         Node(other),
82         body(other.body),
83         use_braces(other.use_braces),
84         parent(0)
85 { }
86
87 void Block::visit(NodeVisitor &visitor)
88 {
89         visitor.visit(*this);
90 }
91
92
93 void Literal::visit(NodeVisitor &visitor)
94 {
95         visitor.visit(*this);
96 }
97
98
99 void ParenthesizedExpression::visit(NodeVisitor &visitor)
100 {
101         visitor.visit(*this);
102 }
103
104
105 VariableReference::VariableReference():
106         declaration(0)
107 { }
108
109 VariableReference::VariableReference(const VariableReference &other):
110         name(other.name),
111         declaration(0)
112 { }
113
114 void VariableReference::visit(NodeVisitor &visitor)
115 {
116         visitor.visit(*this);
117 }
118
119
120 InterfaceBlockReference::InterfaceBlockReference():
121         declaration(0)
122 { }
123
124 InterfaceBlockReference::InterfaceBlockReference(const InterfaceBlockReference &other):
125         name(other.name),
126         declaration(0)
127 { }
128
129 void InterfaceBlockReference::visit(NodeVisitor &visitor)
130 {
131         visitor.visit(*this);
132 }
133
134
135 MemberAccess::MemberAccess():
136         declaration(0)
137 { }
138
139 MemberAccess::MemberAccess(const MemberAccess &other):
140         left(other.left),
141         member(other.member),
142         declaration(0)
143 { }
144
145 void MemberAccess::visit(NodeVisitor &visitor)
146 {
147         visitor.visit(*this);
148 }
149
150
151 UnaryExpression::UnaryExpression():
152         prefix(true)
153 { }
154
155 void UnaryExpression::visit(NodeVisitor &visitor)
156 {
157         visitor.visit(*this);
158 }
159
160
161 void BinaryExpression::visit(NodeVisitor &visitor)
162 {
163         visitor.visit(*this);
164 }
165
166
167 Assignment::Assignment():
168         self_referencing(false),
169         target_declaration(0)
170 { }
171
172 Assignment::Assignment(const Assignment &other):
173         self_referencing(other.self_referencing),
174         target_declaration(0)
175 { }
176
177 void Assignment::visit(NodeVisitor &visitor)
178 {
179         visitor.visit(*this);
180 }
181
182
183 FunctionCall::FunctionCall():
184         constructor(false),
185         declaration(0)
186 { }
187
188 FunctionCall::FunctionCall(const FunctionCall &other):
189         name(other.name),
190         constructor(other.constructor),
191         arguments(other.arguments),
192         declaration(0)
193 { }
194
195 void FunctionCall::visit(NodeVisitor &visitor)
196 {
197         visitor.visit(*this);
198 }
199
200
201 void ExpressionStatement::visit(NodeVisitor &visitor)
202 {
203         visitor.visit(*this);
204 }
205
206
207 void Import::visit(NodeVisitor &visitor)
208 {
209         visitor.visit(*this);
210 }
211
212
213 void Precision::visit(NodeVisitor &visitor)
214 {
215         visitor.visit(*this);
216 }
217
218
219 void Layout::visit(NodeVisitor &visitor)
220 {
221         visitor.visit(*this);
222 }
223
224
225 void InterfaceLayout::visit(NodeVisitor &visitor)
226 {
227         visitor.visit(*this);
228 }
229
230
231 StructDeclaration::StructDeclaration()
232 {
233         members.use_braces = true;
234 }
235
236 void StructDeclaration::visit(NodeVisitor &visitor)
237 {
238         visitor.visit(*this);
239 }
240
241
242 VariableDeclaration::VariableDeclaration():
243         constant(false),
244         array(false),
245         type_declaration(0),
246         linked_declaration(0)
247 { }
248
249 VariableDeclaration::VariableDeclaration(const VariableDeclaration &other):
250         layout(other.layout),
251         constant(other.constant),
252         sampling(other.sampling),
253         interpolation(other.interpolation),
254         interface(other.interface),
255         precision(other.precision),
256         type(other.type),
257         name(other.name),
258         array(other.array),
259         array_size(other.array_size),
260         init_expression(other.init_expression),
261         type_declaration(0),
262         linked_declaration(0)
263 { }
264
265 VariableDeclaration::~VariableDeclaration()
266 {
267         if(linked_declaration && linked_declaration->linked_declaration==this)
268                 linked_declaration->linked_declaration = 0;
269 }
270
271 void VariableDeclaration::visit(NodeVisitor &visitor)
272 {
273         visitor.visit(*this);
274 }
275
276
277 InterfaceBlock::InterfaceBlock():
278         array(false),
279         linked_block(0)
280 {
281         members.use_braces = true;
282 }
283
284 InterfaceBlock::InterfaceBlock(const InterfaceBlock &other):
285         interface(other.interface),
286         name(other.name),
287         members(other.members),
288         instance_name(other.instance_name),
289         array(other.array),
290         linked_block(0)
291 { }
292
293 InterfaceBlock::~InterfaceBlock()
294 {
295         if(linked_block && linked_block->linked_block==this)
296                 linked_block->linked_block = 0;
297 }
298
299 void InterfaceBlock::visit(NodeVisitor &visitor)
300 {
301         visitor.visit(*this);
302 }
303
304
305 FunctionDeclaration::FunctionDeclaration():
306         definition(0)
307 { }
308
309 FunctionDeclaration::FunctionDeclaration(const FunctionDeclaration &other):
310         return_type(other.return_type),
311         name(other.name),
312         parameters(other.parameters),
313         body(other.body),
314         definition(other.definition==&other ? this : 0)
315 { }
316
317 void FunctionDeclaration::visit(NodeVisitor &visitor)
318 {
319         visitor.visit(*this);
320 }
321
322
323 void Conditional::visit(NodeVisitor &visitor)
324 {
325         visitor.visit(*this);
326 }
327
328
329 void Iteration::visit(NodeVisitor &visitor)
330 {
331         visitor.visit(*this);
332 }
333
334
335 void Passthrough::visit(NodeVisitor &visitor)
336 {
337         visitor.visit(*this);
338 }
339
340
341 void Return::visit(NodeVisitor &visitor)
342 {
343         visitor.visit(*this);
344 }
345
346
347 void Jump::visit(NodeVisitor &visitor)
348 {
349         visitor.visit(*this);
350 }
351
352
353 Stage::Stage(Stage::Type t):
354         type(t),
355         previous(0)
356 { }
357
358 const char *Stage::get_stage_name(Type type)
359 {
360         static const char *names[] = { "shared", "vertex", "geometry", "fragment" };
361         return names[type];
362 }
363
364
365 Module::Module():
366         shared(Stage::SHARED)
367 { }
368
369 } // namespace SL
370 } // namespace GL
371 } // namespace Msp