]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/syntax.cpp
Record location information in all syntax nodes
[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 StructDeclaration::StructDeclaration()
241 {
242         members.use_braces = true;
243 }
244
245 void StructDeclaration::visit(NodeVisitor &visitor)
246 {
247         visitor.visit(*this);
248 }
249
250
251 VariableDeclaration::VariableDeclaration():
252         constant(false),
253         array(false),
254         type_declaration(0),
255         linked_declaration(0)
256 { }
257
258 VariableDeclaration::VariableDeclaration(const VariableDeclaration &other):
259         Statement(other),
260         layout(other.layout),
261         constant(other.constant),
262         sampling(other.sampling),
263         interpolation(other.interpolation),
264         interface(other.interface),
265         precision(other.precision),
266         type(other.type),
267         name(other.name),
268         array(other.array),
269         array_size(other.array_size),
270         init_expression(other.init_expression),
271         type_declaration(0),
272         linked_declaration(0)
273 { }
274
275 VariableDeclaration::~VariableDeclaration()
276 {
277         if(linked_declaration && linked_declaration->linked_declaration==this)
278                 linked_declaration->linked_declaration = 0;
279 }
280
281 void VariableDeclaration::visit(NodeVisitor &visitor)
282 {
283         visitor.visit(*this);
284 }
285
286
287 InterfaceBlock::InterfaceBlock():
288         array(false),
289         linked_block(0)
290 {
291         members.use_braces = true;
292 }
293
294 InterfaceBlock::InterfaceBlock(const InterfaceBlock &other):
295         Statement(other),
296         interface(other.interface),
297         name(other.name),
298         members(other.members),
299         instance_name(other.instance_name),
300         array(other.array),
301         linked_block(0)
302 { }
303
304 InterfaceBlock::~InterfaceBlock()
305 {
306         if(linked_block && linked_block->linked_block==this)
307                 linked_block->linked_block = 0;
308 }
309
310 void InterfaceBlock::visit(NodeVisitor &visitor)
311 {
312         visitor.visit(*this);
313 }
314
315
316 FunctionDeclaration::FunctionDeclaration():
317         definition(0)
318 { }
319
320 FunctionDeclaration::FunctionDeclaration(const FunctionDeclaration &other):
321         Statement(other),
322         return_type(other.return_type),
323         name(other.name),
324         parameters(other.parameters),
325         body(other.body),
326         definition(other.definition==&other ? this : 0)
327 { }
328
329 void FunctionDeclaration::visit(NodeVisitor &visitor)
330 {
331         visitor.visit(*this);
332 }
333
334
335 void Conditional::visit(NodeVisitor &visitor)
336 {
337         visitor.visit(*this);
338 }
339
340
341 void Iteration::visit(NodeVisitor &visitor)
342 {
343         visitor.visit(*this);
344 }
345
346
347 void Passthrough::visit(NodeVisitor &visitor)
348 {
349         visitor.visit(*this);
350 }
351
352
353 void Return::visit(NodeVisitor &visitor)
354 {
355         visitor.visit(*this);
356 }
357
358
359 void Jump::visit(NodeVisitor &visitor)
360 {
361         visitor.visit(*this);
362 }
363
364
365 Stage::Stage(Stage::Type t):
366         type(t),
367         previous(0)
368 { }
369
370 const char *Stage::get_stage_name(Type type)
371 {
372         static const char *names[] = { "shared", "vertex", "geometry", "fragment" };
373         return names[type];
374 }
375
376
377 Module::Module():
378         shared(Stage::SHARED)
379 { }
380
381 } // namespace SL
382 } // namespace GL
383 } // namespace Msp