]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/syntax.h
Transform interface block contents into structs
[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 <msp/core/variant.h>
11 #include "features.h"
12 #include "glsl_error.h"
13 #include "sourcemap.h"
14
15 #pragma push_macro("interface")
16 #undef interface
17
18 namespace Msp {
19 namespace GL {
20 namespace SL {
21
22 struct Operator
23 {
24         enum Type
25         {
26                 NO_OPERATOR,
27                 BINARY,
28                 PREFIX,
29                 POSTFIX
30         };
31
32         enum Associativity
33         {
34                 LEFT_TO_RIGHT,
35                 RIGHT_TO_LEFT,
36                 ASSOCIATIVE
37         };
38
39         char token[4];
40         unsigned precedence;
41         Type type;
42         Associativity assoc;
43
44         static const Operator operators[];
45
46         static const Operator &get_operator(const std::string &, Type);
47 };
48
49 enum
50 {
51         INTERNAL_SOURCE = -2,
52         BUILTIN_SOURCE = -1,
53         GENERATED_SOURCE = 0
54 };
55
56 struct NodeVisitor;
57
58 struct Node
59 {
60         int source;
61         unsigned line;
62
63         Node(): source(GENERATED_SOURCE), line(1) { }
64         Node(const Node &n): source(n.source), line(n.line) { }
65 private:
66         Node &operator=(const Node &);
67 public:
68         virtual ~Node() { }
69
70         virtual Node *clone() const = 0;
71         virtual void visit(NodeVisitor &) = 0;
72 };
73
74 template<typename T>
75 class NodePtr: public RefPtr<T>
76 {
77 public:
78         NodePtr() { }
79         NodePtr(T *p): RefPtr<T>(p) { }
80         NodePtr(const NodePtr &p): RefPtr<T>(p ? p->clone() : 0) { }
81         NodePtr &operator=(const NodePtr &p) { RefPtr<T>::operator=(p); return *this; }
82
83         template<typename U>
84         NodePtr(const RefPtr<U> &p): RefPtr<T>(p) { }
85
86         template<typename U>
87         NodePtr(const NodePtr<U> &p): RefPtr<T>(p ? p->clone() : 0) { }
88 };
89
90 template<typename C>
91 class NodeContainer: public C
92 {
93 public:
94         NodeContainer() { }
95         NodeContainer(const NodeContainer &);
96 };
97
98 template<typename T>
99 class NodeList: public NodeContainer<std::list<RefPtr<T> > >
100 { };
101
102 template<typename T>
103 class NodeArray: public NodeContainer<std::vector<RefPtr<T> > >
104 { };
105
106 struct TypeDeclaration;
107 struct VariableDeclaration;
108 struct InterfaceBlock;
109 struct FunctionDeclaration;
110
111 struct Statement: Node
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         TypeDeclaration *type;
136         bool lvalue;
137
138         Expression();
139
140         virtual Expression *clone() const = 0;
141 };
142
143 struct Literal: Expression
144 {
145         std::string token;
146         Variant value;
147
148         virtual Literal *clone() const { return new Literal(*this); }
149         virtual void visit(NodeVisitor &);
150 };
151
152 struct ParenthesizedExpression: Expression
153 {
154         NodePtr<Expression> expression;
155
156         virtual ParenthesizedExpression *clone() const { return new ParenthesizedExpression(*this); }
157         virtual void visit(NodeVisitor &);
158 };
159
160 struct VariableReference: Expression
161 {
162         std::string name;
163
164         VariableDeclaration *declaration;
165
166         VariableReference();
167         VariableReference(const VariableReference &);
168
169         virtual VariableReference *clone() const { return new VariableReference(*this); }
170         virtual void visit(NodeVisitor &);
171 };
172
173 struct InterfaceBlockReference: Expression
174 {
175         std::string name;
176
177         InterfaceBlock *declaration;
178
179         InterfaceBlockReference();
180         InterfaceBlockReference(const InterfaceBlockReference &);
181
182         virtual InterfaceBlockReference *clone() const { return new InterfaceBlockReference(*this); }
183         virtual void visit(NodeVisitor &);
184 };
185
186 struct MemberAccess: Expression
187 {
188         NodePtr<Expression> left;
189         std::string member;
190
191         VariableDeclaration *declaration;
192
193         MemberAccess();
194         MemberAccess(const MemberAccess &);
195
196         virtual MemberAccess *clone() const { return new MemberAccess(*this); }
197         virtual void visit(NodeVisitor &);
198 };
199
200 struct UnaryExpression: Expression
201 {
202         NodePtr<Expression> expression;
203
204         virtual UnaryExpression *clone() const { return new UnaryExpression(*this); }
205         virtual void visit(NodeVisitor &);
206 };
207
208 struct BinaryExpression: Expression
209 {
210         NodePtr<Expression> left;
211         NodePtr<Expression> right;
212
213         virtual BinaryExpression *clone() const { return new BinaryExpression(*this); }
214         virtual void visit(NodeVisitor &);
215 };
216
217 struct Assignment: BinaryExpression
218 {
219         bool self_referencing;
220
221         VariableDeclaration *target_declaration;
222
223         Assignment();
224         Assignment(const Assignment &);
225
226         virtual Assignment *clone() const { return new Assignment(*this); }
227         virtual void visit(NodeVisitor &);
228 };
229
230 struct FunctionCall: Expression
231 {
232         std::string name;
233         bool constructor;
234         NodeArray<Expression> arguments;
235
236         FunctionDeclaration *declaration;
237
238         FunctionCall();
239         FunctionCall(const FunctionCall &);
240
241         virtual FunctionCall *clone() const { return new FunctionCall(*this); }
242         virtual void visit(NodeVisitor &);
243 };
244
245 struct ExpressionStatement: Statement
246 {
247         NodePtr<Expression> expression;
248
249         virtual ExpressionStatement *clone() const { return new ExpressionStatement(*this); }
250         virtual void visit(NodeVisitor &);
251 };
252
253 struct Import: Statement
254 {
255         std::string module;
256
257         virtual Import *clone() const { return new Import(*this); }
258         virtual void visit(NodeVisitor &);
259 };
260
261 struct Precision: Statement
262 {
263         std::string precision;
264         std::string type;
265
266         virtual Precision *clone() const { return new Precision(*this); }
267         virtual void visit(NodeVisitor &);
268 };
269
270 struct Layout: Node
271 {
272         struct Qualifier
273         {
274                 std::string name;
275                 bool has_value;
276                 int value;
277         };
278
279         std::vector<Qualifier> qualifiers;
280
281         virtual Layout *clone() const { return new Layout(*this); }
282         virtual void visit(NodeVisitor &);
283 };
284
285 struct InterfaceLayout: Statement
286 {
287         std::string interface;
288         Layout layout;
289
290         virtual InterfaceLayout *clone() const { return new InterfaceLayout(*this); }
291         virtual void visit(NodeVisitor &);
292 };
293
294 struct TypeDeclaration: Statement
295 {
296         std::string name;
297
298         virtual TypeDeclaration *clone() const = 0;
299 };
300
301 struct BasicTypeDeclaration: TypeDeclaration
302 {
303         enum Kind
304         {
305                 ALIAS,
306                 VOID,
307                 BOOL,
308                 INT,
309                 FLOAT,
310                 VECTOR,
311                 MATRIX,
312                 ARRAY
313         };
314
315         Kind kind;
316         unsigned size;
317         std::string base;
318
319         TypeDeclaration *base_type;
320
321         BasicTypeDeclaration();
322         BasicTypeDeclaration(const BasicTypeDeclaration &);
323
324         virtual BasicTypeDeclaration *clone() const { return new BasicTypeDeclaration(*this); }
325         virtual void visit(NodeVisitor &);
326 };
327
328 struct ImageTypeDeclaration: TypeDeclaration
329 {
330         enum Dimensions
331         {
332                 ONE = 1,
333                 TWO,
334                 THREE,
335                 CUBE
336         };
337
338         Dimensions dimensions;
339         bool array;
340         bool sampled;
341         bool shadow;
342         std::string base;
343
344         TypeDeclaration *base_type;
345
346         ImageTypeDeclaration();
347
348         virtual ImageTypeDeclaration *clone() const { return new ImageTypeDeclaration(*this); }
349         virtual void visit(NodeVisitor &);
350 };
351
352 struct StructDeclaration: TypeDeclaration
353 {
354         Block members;
355
356         InterfaceBlock *interface_block;
357
358         StructDeclaration();
359         StructDeclaration(const StructDeclaration &);
360         ~StructDeclaration();
361
362         virtual StructDeclaration *clone() const { return new StructDeclaration(*this); }
363         virtual void visit(NodeVisitor &);
364 };
365
366 struct VariableDeclaration: Statement
367 {
368         NodePtr<Layout> layout;
369         bool constant;
370         std::string sampling;
371         std::string interpolation;
372         std::string interface;
373         std::string precision;
374         std::string type;
375         std::string name;
376         bool array;
377         NodePtr<Expression> array_size;
378         NodePtr<Expression> init_expression;
379
380         TypeDeclaration *type_declaration;
381         VariableDeclaration *linked_declaration;
382
383         VariableDeclaration();
384         VariableDeclaration(const VariableDeclaration &);
385         ~VariableDeclaration();
386
387         virtual VariableDeclaration *clone() const { return new VariableDeclaration(*this); }
388         virtual void visit(NodeVisitor &);
389 };
390
391 struct InterfaceBlock: Statement
392 {
393         std::string interface;
394         std::string name;
395         NodePtr<Block> members;
396         std::string instance_name;
397         bool array;
398
399         /* An interface block's ultimate base type is always a struct.  The
400         immediate type may be either that same struct or an array of it. */
401         TypeDeclaration *type_declaration;
402         StructDeclaration *struct_declaration;
403         InterfaceBlock *linked_block;
404
405         InterfaceBlock();
406         InterfaceBlock(const InterfaceBlock &);
407         ~InterfaceBlock();
408
409         virtual InterfaceBlock *clone() const { return new InterfaceBlock(*this); }
410         virtual void visit(NodeVisitor &);
411 };
412
413 struct FunctionDeclaration: Statement
414 {
415         std::string return_type;
416         std::string name;
417         NodeArray<VariableDeclaration> parameters;
418         Block body;
419
420         FunctionDeclaration *definition;
421         TypeDeclaration *return_type_declaration;
422
423         FunctionDeclaration();
424         FunctionDeclaration(const FunctionDeclaration &);
425
426         virtual FunctionDeclaration *clone() const { return new FunctionDeclaration(*this); }
427         virtual void visit(NodeVisitor &);
428 };
429
430 struct Conditional: Statement
431 {
432         NodePtr<Expression> condition;
433         Block body;
434         Block else_body;
435
436         virtual Conditional *clone() const { return new Conditional(*this); }
437         virtual void visit(NodeVisitor &);
438 };
439
440 struct Iteration: Statement
441 {
442         NodePtr<Statement> init_statement;
443         NodePtr<Expression> condition;
444         NodePtr<Expression> loop_expression;
445         Block body;
446
447         virtual Iteration *clone() const { return new Iteration(*this); }
448         virtual void visit(NodeVisitor &);
449 };
450
451 struct Passthrough: Statement
452 {
453         NodePtr<Expression> subscript;
454
455         virtual Passthrough *clone() const { return new Passthrough(*this); }
456         virtual void visit(NodeVisitor &);
457 };
458
459 struct Return: Statement
460 {
461         NodePtr<Expression> expression;
462
463         virtual Return *clone() const { return new Return(*this); }
464         virtual void visit(NodeVisitor &);
465 };
466
467 struct Jump: Statement
468 {
469         std::string keyword;
470
471         virtual Jump *clone() const { return new Jump(*this); }
472         virtual void visit(NodeVisitor &);
473 };
474
475 struct Stage
476 {
477         enum Type
478         {
479                 SHARED,
480                 VERTEX,
481                 GEOMETRY,
482                 FRAGMENT
483         };
484
485         Type type;
486         Stage *previous;
487         Block content;
488         std::map<std::string, TypeDeclaration *> types;
489         std::map<std::string, InterfaceBlock *> interface_blocks;
490         std::map<std::string, FunctionDeclaration *> functions;
491         std::map<std::string, unsigned> locations;
492         Features required_features;
493         std::vector<Diagnostic> diagnostics;
494
495         Stage(Type);
496
497         static const char *get_stage_name(Type);
498 };
499
500 struct Module
501 {
502         SourceMap source_map;
503         Stage shared;
504         std::list<Stage> stages;
505
506         Module();
507 };
508
509 } // namespace SL
510 } // namespace GL
511 } // namespace Msp
512
513 #pragma pop_macro("interface")
514
515 #endif