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