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