]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/syntax.h
Add a new associativity type for fully associative operators
[libs/gl.git] / source / glsl / syntax.h
1 #ifndef MSP_GL_SL_SYNTAX_H_
2 #define MSP_GL_SL_SYNTAX_H_
3
4 #include <list>
5 #include <map>
6 #include <set>
7 #include <string>
8 #include <vector>
9 #include <msp/core/refptr.h>
10 #include "features.h"
11 #include "sourcemap.h"
12
13 #pragma push_macro("interface")
14 #undef interface
15
16 namespace Msp {
17 namespace GL {
18 namespace SL {
19
20 struct Operator
21 {
22         enum Type
23         {
24                 NO_OPERATOR,
25                 BINARY,
26                 PREFIX,
27                 POSTFIX
28         };
29
30         enum Associativity
31         {
32                 LEFT_TO_RIGHT,
33                 RIGHT_TO_LEFT,
34                 ASSOCIATIVE
35         };
36
37         char token[4];
38         unsigned precedence;
39         Type type;
40         Associativity assoc;
41
42         static const Operator operators[];
43
44         static const Operator &get_operator(const std::string &, Type);
45 };
46
47 enum
48 {
49         BUILTIN_SOURCE = -1,
50         GENERATED_SOURCE = 0
51 };
52
53 struct NodeVisitor;
54
55 struct Node
56 {
57 protected:
58         Node() { }
59         Node(const Node &) { }
60 private:
61         Node &operator=(const Node &);
62 public:
63         virtual ~Node() { }
64
65         virtual Node *clone() const = 0;
66         virtual void visit(NodeVisitor &) = 0;
67 };
68
69 template<typename T>
70 class NodePtr: public RefPtr<T>
71 {
72 public:
73         NodePtr() { }
74         NodePtr(T *p): RefPtr<T>(p) { }
75         NodePtr(const NodePtr &p): RefPtr<T>(p ? p->clone() : 0) { }
76         NodePtr &operator=(const NodePtr &p) { RefPtr<T>::operator=(p); return *this; }
77
78         template<typename U>
79         NodePtr(const RefPtr<U> &p): RefPtr<T>(p) { }
80
81         template<typename U>
82         NodePtr(const NodePtr<U> &p): RefPtr<T>(p ? p->clone() : 0) { }
83 };
84
85 template<typename C>
86 class NodeContainer: public C
87 {
88 public:
89         NodeContainer() { }
90         NodeContainer(const NodeContainer &);
91 };
92
93 template<typename T>
94 class NodeList: public NodeContainer<std::list<RefPtr<T> > >
95 { };
96
97 template<typename T>
98 class NodeArray: public NodeContainer<std::vector<RefPtr<T> > >
99 { };
100
101 struct StructDeclaration;
102 struct VariableDeclaration;
103 struct InterfaceBlock;
104 struct FunctionDeclaration;
105
106 struct Statement: Node
107 {
108         int source;
109         unsigned line;
110
111         Statement();
112
113         virtual Statement *clone() const = 0;
114 };
115
116 struct Block: Node
117 {
118         NodeList<Statement> body;
119         bool use_braces;
120
121         std::map<std::string, VariableDeclaration *> variables;
122         Block *parent;
123
124         Block();
125         Block(const Block &);
126
127         virtual Block *clone() const { return new Block(*this); }
128         virtual void visit(NodeVisitor &);
129 };
130
131 struct Expression: Node
132 {
133         const Operator *oper;
134
135         Expression();
136
137         virtual Expression *clone() const = 0;
138 };
139
140 struct Literal: Expression
141 {
142         std::string token;
143
144         virtual Literal *clone() const { return new Literal(*this); }
145         virtual void visit(NodeVisitor &);
146 };
147
148 struct ParenthesizedExpression: Expression
149 {
150         NodePtr<Expression> expression;
151
152         virtual ParenthesizedExpression *clone() const { return new ParenthesizedExpression(*this); }
153         virtual void visit(NodeVisitor &);
154 };
155
156 struct VariableReference: Expression
157 {
158         std::string name;
159
160         VariableDeclaration *declaration;
161
162         VariableReference();
163         VariableReference(const VariableReference &);
164
165         virtual VariableReference *clone() const { return new VariableReference(*this); }
166         virtual void visit(NodeVisitor &);
167 };
168
169 struct InterfaceBlockReference: Expression
170 {
171         std::string name;
172
173         InterfaceBlock *declaration;
174
175         InterfaceBlockReference();
176         InterfaceBlockReference(const InterfaceBlockReference &);
177
178         virtual InterfaceBlockReference *clone() const { return new InterfaceBlockReference(*this); }
179         virtual void visit(NodeVisitor &);
180 };
181
182 struct MemberAccess: Expression
183 {
184         NodePtr<Expression> left;
185         std::string member;
186
187         VariableDeclaration *declaration;
188
189         MemberAccess();
190         MemberAccess(const MemberAccess &);
191
192         virtual MemberAccess *clone() const { return new MemberAccess(*this); }
193         virtual void visit(NodeVisitor &);
194 };
195
196 struct UnaryExpression: Expression
197 {
198         NodePtr<Expression> expression;
199
200         virtual UnaryExpression *clone() const { return new UnaryExpression(*this); }
201         virtual void visit(NodeVisitor &);
202 };
203
204 struct BinaryExpression: Expression
205 {
206         NodePtr<Expression> left;
207         NodePtr<Expression> right;
208
209         virtual BinaryExpression *clone() const { return new BinaryExpression(*this); }
210         virtual void visit(NodeVisitor &);
211 };
212
213 struct Assignment: BinaryExpression
214 {
215         bool self_referencing;
216
217         VariableDeclaration *target_declaration;
218
219         Assignment();
220         Assignment(const Assignment &);
221
222         virtual Assignment *clone() const { return new Assignment(*this); }
223         virtual void visit(NodeVisitor &);
224 };
225
226 struct FunctionCall: Expression
227 {
228         std::string name;
229         bool constructor;
230         NodeArray<Expression> arguments;
231
232         FunctionDeclaration *declaration;
233
234         FunctionCall();
235         FunctionCall(const FunctionCall &);
236
237         virtual FunctionCall *clone() const { return new FunctionCall(*this); }
238         virtual void visit(NodeVisitor &);
239 };
240
241 struct ExpressionStatement: Statement
242 {
243         NodePtr<Expression> expression;
244
245         virtual ExpressionStatement *clone() const { return new ExpressionStatement(*this); }
246         virtual void visit(NodeVisitor &);
247 };
248
249 struct Import: Statement
250 {
251         std::string module;
252
253         virtual Import *clone() const { return new Import(*this); }
254         virtual void visit(NodeVisitor &);
255 };
256
257 struct Precision: Statement
258 {
259         std::string precision;
260         std::string type;
261
262         virtual Precision *clone() const { return new Precision(*this); }
263         virtual void visit(NodeVisitor &);
264 };
265
266 struct Layout: Node
267 {
268         struct Qualifier
269         {
270                 std::string name;
271                 bool has_value;
272                 int value;
273         };
274
275         std::vector<Qualifier> qualifiers;
276
277         virtual Layout *clone() const { return new Layout(*this); }
278         virtual void visit(NodeVisitor &);
279 };
280
281 struct InterfaceLayout: Statement
282 {
283         std::string interface;
284         Layout layout;
285
286         virtual InterfaceLayout *clone() const { return new InterfaceLayout(*this); }
287         virtual void visit(NodeVisitor &);
288 };
289
290 struct StructDeclaration: Statement
291 {
292         std::string name;
293         Block members;
294
295         StructDeclaration();
296
297         virtual StructDeclaration *clone() const { return new StructDeclaration(*this); }
298         virtual void visit(NodeVisitor &);
299 };
300
301 struct VariableDeclaration: Statement
302 {
303         NodePtr<Layout> layout;
304         bool constant;
305         std::string sampling;
306         std::string interpolation;
307         std::string interface;
308         std::string precision;
309         std::string type;
310         std::string name;
311         bool array;
312         NodePtr<Expression> array_size;
313         NodePtr<Expression> init_expression;
314
315         StructDeclaration *type_declaration;
316         VariableDeclaration *linked_declaration;
317
318         VariableDeclaration();
319         VariableDeclaration(const VariableDeclaration &);
320         ~VariableDeclaration();
321
322         virtual VariableDeclaration *clone() const { return new VariableDeclaration(*this); }
323         virtual void visit(NodeVisitor &);
324 };
325
326 struct InterfaceBlock: Statement
327 {
328         std::string interface;
329         std::string name;
330         Block members;
331         std::string instance_name;
332         bool array;
333
334         InterfaceBlock *linked_block;
335
336         InterfaceBlock();
337         InterfaceBlock(const InterfaceBlock &);
338         ~InterfaceBlock();
339
340         virtual InterfaceBlock *clone() const { return new InterfaceBlock(*this); }
341         virtual void visit(NodeVisitor &);
342 };
343
344 struct FunctionDeclaration: Statement
345 {
346         std::string return_type;
347         std::string name;
348         NodeArray<VariableDeclaration> parameters;
349         Block body;
350
351         FunctionDeclaration *definition;
352
353         FunctionDeclaration();
354         FunctionDeclaration(const FunctionDeclaration &);
355
356         virtual FunctionDeclaration *clone() const { return new FunctionDeclaration(*this); }
357         virtual void visit(NodeVisitor &);
358 };
359
360 struct Conditional: Statement
361 {
362         NodePtr<Expression> condition;
363         Block body;
364         Block else_body;
365
366         virtual Conditional *clone() const { return new Conditional(*this); }
367         virtual void visit(NodeVisitor &);
368 };
369
370 struct Iteration: Statement
371 {
372         NodePtr<Node> init_statement;
373         NodePtr<Expression> condition;
374         NodePtr<Expression> loop_expression;
375         Block body;
376
377         virtual Iteration *clone() const { return new Iteration(*this); }
378         virtual void visit(NodeVisitor &);
379 };
380
381 struct Passthrough: Statement
382 {
383         NodePtr<Expression> subscript;
384
385         virtual Passthrough *clone() const { return new Passthrough(*this); }
386         virtual void visit(NodeVisitor &);
387 };
388
389 struct Return: Statement
390 {
391         NodePtr<Expression> expression;
392
393         virtual Return *clone() const { return new Return(*this); }
394         virtual void visit(NodeVisitor &);
395 };
396
397 struct Jump: Statement
398 {
399         std::string keyword;
400
401         virtual Jump *clone() const { return new Jump(*this); }
402         virtual void visit(NodeVisitor &);
403 };
404
405 struct Stage
406 {
407         enum Type
408         {
409                 SHARED,
410                 VERTEX,
411                 GEOMETRY,
412                 FRAGMENT
413         };
414
415         Type type;
416         Stage *previous;
417         Block content;
418         std::map<std::string, StructDeclaration *> types;
419         std::map<std::string, InterfaceBlock *> interface_blocks;
420         std::map<std::string, FunctionDeclaration *> functions;
421         std::map<std::string, unsigned> locations;
422         Features required_features;
423
424         Stage(Type);
425
426         static const char *get_stage_name(Type);
427 };
428
429 struct Module
430 {
431         SourceMap source_map;
432         Stage shared;
433         std::list<Stage> stages;
434
435         Module();
436 };
437
438 } // namespace SL
439 } // namespace GL
440 } // namespace Msp
441
442 #pragma pop_macro("interface")
443
444 #endif