]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/syntax.h
1caf4d9568c10f5b3c45faed04fd25cab14a606b
[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         NodePtr<Layout> layout;
433         std::string interface;
434         std::string block_name;
435         NodePtr<Block> members;
436         std::string instance_name;
437         bool array;
438
439         /* An interface block's ultimate base type is always a struct.  The
440         immediate type may be either that same struct or an array of it. */
441         TypeDeclaration *type_declaration;
442         StructDeclaration *struct_declaration;
443         InterfaceBlock *linked_block;
444
445         InterfaceBlock();
446         InterfaceBlock(const InterfaceBlock &);
447         ~InterfaceBlock();
448
449         virtual InterfaceBlock *clone() const { return new InterfaceBlock(*this); }
450         virtual void visit(NodeVisitor &);
451 };
452
453 struct FunctionDeclaration: Statement
454 {
455         std::string return_type;
456         std::string name;
457         NodeArray<VariableDeclaration> parameters;
458         bool virtua;
459         bool overrd;
460         Block body;
461
462         std::string signature;
463         FunctionDeclaration *definition;
464         TypeDeclaration *return_type_declaration;
465
466         FunctionDeclaration();
467         FunctionDeclaration(const FunctionDeclaration &);
468
469         virtual FunctionDeclaration *clone() const { return new FunctionDeclaration(*this); }
470         virtual void visit(NodeVisitor &);
471 };
472
473 struct Conditional: Statement
474 {
475         NodePtr<Expression> condition;
476         Block body;
477         Block else_body;
478
479         virtual Conditional *clone() const { return new Conditional(*this); }
480         virtual void visit(NodeVisitor &);
481 };
482
483 struct Iteration: Statement
484 {
485         NodePtr<Statement> init_statement;
486         NodePtr<Expression> condition;
487         NodePtr<Expression> loop_expression;
488         Block body;
489
490         virtual Iteration *clone() const { return new Iteration(*this); }
491         virtual void visit(NodeVisitor &);
492 };
493
494 struct Passthrough: Statement
495 {
496         NodePtr<Expression> subscript;
497
498         virtual Passthrough *clone() const { return new Passthrough(*this); }
499         virtual void visit(NodeVisitor &);
500 };
501
502 struct Return: Statement
503 {
504         NodePtr<Expression> expression;
505
506         virtual Return *clone() const { return new Return(*this); }
507         virtual void visit(NodeVisitor &);
508 };
509
510 struct Jump: Statement
511 {
512         std::string keyword;
513
514         virtual Jump *clone() const { return new Jump(*this); }
515         virtual void visit(NodeVisitor &);
516 };
517
518 struct Stage
519 {
520         enum Type
521         {
522                 SHARED,
523                 VERTEX,
524                 GEOMETRY,
525                 FRAGMENT
526         };
527
528         Type type;
529         Stage *previous;
530         Block content;
531         std::map<std::string, TypeDeclaration *> types;
532         std::map<std::string, InterfaceBlock *> interface_blocks;
533         std::map<std::string, FunctionDeclaration *> functions;
534         std::map<std::string, unsigned> locations;
535         Features required_features;
536         std::vector<Diagnostic> diagnostics;
537
538         Stage(Type);
539
540         static const char *get_stage_name(Type);
541 };
542
543 struct Module
544 {
545         SourceMap source_map;
546         Stage shared;
547         std::list<Stage> stages;
548
549         Module();
550 };
551
552 std::string get_unused_variable_name(const Block &, const std::string &);
553
554 } // namespace SL
555 } // namespace GL
556 } // namespace Msp
557
558 #pragma pop_macro("interface")
559
560 #endif