]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/syntax.h
b4d1b3869ae9395c90a5a424fa939ac17fada381
[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                 Qualifier(): has_value(false), value(0) { }
318         };
319
320         std::vector<Qualifier> qualifiers;
321
322         virtual Layout *clone() const { return new Layout(*this); }
323         virtual void visit(NodeVisitor &);
324 };
325
326 struct InterfaceLayout: Statement
327 {
328         std::string interface;
329         Layout layout;
330
331         virtual InterfaceLayout *clone() const { return new InterfaceLayout(*this); }
332         virtual void visit(NodeVisitor &);
333 };
334
335 struct TypeDeclaration: Statement
336 {
337         std::string name;
338
339         virtual TypeDeclaration *clone() const = 0;
340 };
341
342 struct BasicTypeDeclaration: TypeDeclaration
343 {
344         enum Kind
345         {
346                 ALIAS,
347                 VOID,
348                 BOOL,
349                 INT,
350                 FLOAT,
351                 VECTOR,
352                 MATRIX,
353                 ARRAY
354         };
355
356         Kind kind;
357         unsigned size;
358         std::string base;
359
360         TypeDeclaration *base_type;
361
362         BasicTypeDeclaration();
363         BasicTypeDeclaration(const BasicTypeDeclaration &);
364
365         virtual BasicTypeDeclaration *clone() const { return new BasicTypeDeclaration(*this); }
366         virtual void visit(NodeVisitor &);
367 };
368
369 struct ImageTypeDeclaration: TypeDeclaration
370 {
371         enum Dimensions
372         {
373                 ONE = 1,
374                 TWO,
375                 THREE,
376                 CUBE
377         };
378
379         Dimensions dimensions;
380         bool array;
381         bool sampled;
382         bool shadow;
383         std::string base;
384
385         TypeDeclaration *base_type;
386
387         ImageTypeDeclaration();
388
389         virtual ImageTypeDeclaration *clone() const { return new ImageTypeDeclaration(*this); }
390         virtual void visit(NodeVisitor &);
391 };
392
393 struct StructDeclaration: TypeDeclaration
394 {
395         Block members;
396
397         InterfaceBlock *interface_block;
398
399         StructDeclaration();
400         StructDeclaration(const StructDeclaration &);
401         ~StructDeclaration();
402
403         virtual StructDeclaration *clone() const { return new StructDeclaration(*this); }
404         virtual void visit(NodeVisitor &);
405 };
406
407 struct VariableDeclaration: Statement
408 {
409         NodePtr<Layout> layout;
410         bool constant;
411         std::string sampling;
412         std::string interpolation;
413         std::string interface;
414         std::string precision;
415         std::string type;
416         std::string name;
417         bool array;
418         NodePtr<Expression> array_size;
419         NodePtr<Expression> init_expression;
420
421         TypeDeclaration *type_declaration;
422         VariableDeclaration *linked_declaration;
423
424         VariableDeclaration();
425         VariableDeclaration(const VariableDeclaration &);
426         ~VariableDeclaration();
427
428         virtual VariableDeclaration *clone() const { return new VariableDeclaration(*this); }
429         virtual void visit(NodeVisitor &);
430 };
431
432 struct InterfaceBlock: Statement
433 {
434         NodePtr<Layout> layout;
435         std::string interface;
436         std::string block_name;
437         NodePtr<Block> members;
438         std::string instance_name;
439         bool array;
440
441         /* An interface block's ultimate base type is always a struct.  The
442         immediate type may be either that same struct or an array of it. */
443         TypeDeclaration *type_declaration;
444         StructDeclaration *struct_declaration;
445         InterfaceBlock *linked_block;
446
447         InterfaceBlock();
448         InterfaceBlock(const InterfaceBlock &);
449         ~InterfaceBlock();
450
451         virtual InterfaceBlock *clone() const { return new InterfaceBlock(*this); }
452         virtual void visit(NodeVisitor &);
453 };
454
455 struct FunctionDeclaration: Statement
456 {
457         std::string return_type;
458         std::string name;
459         NodeArray<VariableDeclaration> parameters;
460         bool virtua;
461         bool overrd;
462         Block body;
463
464         std::string signature;
465         FunctionDeclaration *definition;
466         TypeDeclaration *return_type_declaration;
467
468         FunctionDeclaration();
469         FunctionDeclaration(const FunctionDeclaration &);
470
471         virtual FunctionDeclaration *clone() const { return new FunctionDeclaration(*this); }
472         virtual void visit(NodeVisitor &);
473 };
474
475 struct Conditional: Statement
476 {
477         NodePtr<Expression> condition;
478         Block body;
479         Block else_body;
480
481         virtual Conditional *clone() const { return new Conditional(*this); }
482         virtual void visit(NodeVisitor &);
483 };
484
485 struct Iteration: Statement
486 {
487         NodePtr<Statement> init_statement;
488         NodePtr<Expression> condition;
489         NodePtr<Expression> loop_expression;
490         Block body;
491
492         virtual Iteration *clone() const { return new Iteration(*this); }
493         virtual void visit(NodeVisitor &);
494 };
495
496 struct Passthrough: Statement
497 {
498         NodePtr<Expression> subscript;
499
500         virtual Passthrough *clone() const { return new Passthrough(*this); }
501         virtual void visit(NodeVisitor &);
502 };
503
504 struct Return: Statement
505 {
506         NodePtr<Expression> expression;
507
508         virtual Return *clone() const { return new Return(*this); }
509         virtual void visit(NodeVisitor &);
510 };
511
512 struct Jump: Statement
513 {
514         std::string keyword;
515
516         virtual Jump *clone() const { return new Jump(*this); }
517         virtual void visit(NodeVisitor &);
518 };
519
520 struct Stage
521 {
522         enum Type
523         {
524                 SHARED,
525                 VERTEX,
526                 GEOMETRY,
527                 FRAGMENT
528         };
529
530         Type type;
531         Stage *previous;
532         Block content;
533         std::map<std::string, TypeDeclaration *> types;
534         std::map<std::string, InterfaceBlock *> interface_blocks;
535         std::map<std::string, FunctionDeclaration *> functions;
536         std::map<std::string, unsigned> locations;
537         std::map<std::string, unsigned> texture_bindings;
538         std::map<std::string, unsigned> uniform_block_bindings;
539         Features required_features;
540         std::vector<Diagnostic> diagnostics;
541
542         Stage(Type);
543
544         static const char *get_stage_name(Type);
545 };
546
547 struct Module
548 {
549         SourceMap source_map;
550         Stage shared;
551         std::list<Stage> stages;
552
553         Module();
554 };
555
556 std::string get_unused_variable_name(const Block &, const std::string &);
557
558 bool is_same_type(const TypeDeclaration &, const TypeDeclaration &);
559 int get_layout_value(const Layout &, const std::string &, int = -1);
560
561 } // namespace SL
562 } // namespace GL
563 } // namespace Msp
564
565 #pragma pop_macro("interface")
566
567 #endif