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