]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/syntax.cpp
Improve support for interface blocks
[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(0),
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         declaration(0),
185         constructor(false)
186 { }
187
188 FunctionCall::FunctionCall(const FunctionCall &other):
189         name(other.name),
190         declaration(0),
191         constructor(other.constructor),
192         arguments(other.arguments)
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         type_declaration(0),
245         array(false),
246         linked_declaration(0)
247 { }
248
249 VariableDeclaration::VariableDeclaration(const VariableDeclaration &other):
250         constant(other.constant),
251         sampling(other.sampling),
252         interpolation(other.interpolation),
253         interface(other.interface),
254         precision(other.precision),
255         type(other.type),
256         type_declaration(0),
257         name(other.name),
258         array(other.array),
259         array_size(other.array_size),
260         init_expression(other.init_expression),
261         linked_declaration(0),
262         layout(other.layout)
263 { }
264
265 void VariableDeclaration::visit(NodeVisitor &visitor)
266 {
267         visitor.visit(*this);
268 }
269
270
271 InterfaceBlock::InterfaceBlock():
272         array(false),
273         linked_block(0)
274 {
275         members.use_braces = true;
276 }
277
278 InterfaceBlock::InterfaceBlock(const InterfaceBlock &other):
279         interface(other.interface),
280         name(other.name),
281         members(other.members),
282         instance_name(other.instance_name),
283         array(other.array),
284         linked_block(0)
285 { }
286
287 void InterfaceBlock::visit(NodeVisitor &visitor)
288 {
289         visitor.visit(*this);
290 }
291
292
293 FunctionDeclaration::FunctionDeclaration():
294         definition(0)
295 { }
296
297 FunctionDeclaration::FunctionDeclaration(const FunctionDeclaration &other):
298         return_type(other.return_type),
299         name(other.name),
300         parameters(other.parameters),
301         definition(other.definition==&other ? this : 0),
302         body(other.body)
303 { }
304
305 void FunctionDeclaration::visit(NodeVisitor &visitor)
306 {
307         visitor.visit(*this);
308 }
309
310
311 void Conditional::visit(NodeVisitor &visitor)
312 {
313         visitor.visit(*this);
314 }
315
316
317 void Iteration::visit(NodeVisitor &visitor)
318 {
319         visitor.visit(*this);
320 }
321
322
323 void Passthrough::visit(NodeVisitor &visitor)
324 {
325         visitor.visit(*this);
326 }
327
328
329 void Return::visit(NodeVisitor &visitor)
330 {
331         visitor.visit(*this);
332 }
333
334
335 void Jump::visit(NodeVisitor &visitor)
336 {
337         visitor.visit(*this);
338 }
339
340
341 Stage::Stage(Stage::Type t):
342         type(t),
343         previous(0)
344 { }
345
346 const char *Stage::get_stage_name(Type type)
347 {
348         static const char *names[] = { "shared", "vertex", "geometry", "fragment" };
349         return names[type];
350 }
351
352
353 Module::Module():
354         shared(Stage::SHARED)
355 { }
356
357 } // namespace SL
358 } // namespace GL
359 } // namespace Msp