]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/syntax.h
3518cb4c3e3145b281661aad152be938120cc556
[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         int index;
191
192         MemberAccess();
193         MemberAccess(const MemberAccess &);
194
195         virtual MemberAccess *clone() const { return new MemberAccess(*this); }
196         virtual void visit(NodeVisitor &);
197 };
198
199 struct Swizzle: Expression
200 {
201         NodePtr<Expression> left;
202         std::string component_group;
203         unsigned count;
204         UInt8 components[4];
205
206         Swizzle();
207
208         virtual Swizzle *clone() const { return new Swizzle(*this); }
209         virtual void visit(NodeVisitor &);
210 };
211
212 struct UnaryExpression: Expression
213 {
214         NodePtr<Expression> expression;
215
216         virtual UnaryExpression *clone() const { return new UnaryExpression(*this); }
217         virtual void visit(NodeVisitor &);
218 };
219
220 struct BinaryExpression: Expression
221 {
222         NodePtr<Expression> left;
223         NodePtr<Expression> right;
224
225         virtual BinaryExpression *clone() const { return new BinaryExpression(*this); }
226         virtual void visit(NodeVisitor &);
227 };
228
229 struct Assignment: BinaryExpression
230 {
231         struct Target
232         {
233                 enum ChainType
234                 {
235                         MEMBER = 0x40,
236                         SWIZZLE = 0x80,
237                         ARRAY = 0xC0
238                 };
239
240                 Statement *declaration;
241                 Msp::UInt8 chain_len;
242                 Msp::UInt8 chain[7];
243
244                 Target(Statement * = 0);
245
246                 bool operator<(const Target &) const;
247         };
248
249         bool self_referencing;
250
251         Target target;
252
253         Assignment();
254         Assignment(const Assignment &);
255
256         virtual Assignment *clone() const { return new Assignment(*this); }
257         virtual void visit(NodeVisitor &);
258 };
259
260 struct TernaryExpression: Expression
261 {
262         NodePtr<Expression> condition;
263         NodePtr<Expression> true_expr;
264         NodePtr<Expression> false_expr;
265
266         virtual TernaryExpression *clone() const { return new TernaryExpression(*this); }
267         virtual void visit(NodeVisitor &);
268 };
269
270 struct FunctionCall: Expression
271 {
272         std::string name;
273         bool constructor;
274         NodeArray<Expression> arguments;
275
276         FunctionDeclaration *declaration;
277
278         FunctionCall();
279         FunctionCall(const FunctionCall &);
280
281         virtual FunctionCall *clone() const { return new FunctionCall(*this); }
282         virtual void visit(NodeVisitor &);
283 };
284
285 struct ExpressionStatement: Statement
286 {
287         NodePtr<Expression> expression;
288
289         virtual ExpressionStatement *clone() const { return new ExpressionStatement(*this); }
290         virtual void visit(NodeVisitor &);
291 };
292
293 struct Import: Statement
294 {
295         std::string module;
296
297         virtual Import *clone() const { return new Import(*this); }
298         virtual void visit(NodeVisitor &);
299 };
300
301 struct Precision: Statement
302 {
303         std::string precision;
304         std::string type;
305
306         virtual Precision *clone() const { return new Precision(*this); }
307         virtual void visit(NodeVisitor &);
308 };
309
310 struct Layout: Node
311 {
312         struct Qualifier
313         {
314                 std::string name;
315                 bool has_value;
316                 int value;
317
318                 Qualifier(): has_value(false), value(0) { }
319         };
320
321         std::vector<Qualifier> qualifiers;
322
323         virtual Layout *clone() const { return new Layout(*this); }
324         virtual void visit(NodeVisitor &);
325 };
326
327 struct InterfaceLayout: Statement
328 {
329         std::string interface;
330         Layout layout;
331
332         virtual InterfaceLayout *clone() const { return new InterfaceLayout(*this); }
333         virtual void visit(NodeVisitor &);
334 };
335
336 struct TypeDeclaration: Statement
337 {
338         std::string name;
339
340         virtual TypeDeclaration *clone() const = 0;
341 };
342
343 struct BasicTypeDeclaration: TypeDeclaration
344 {
345         enum Kind
346         {
347                 ALIAS,
348                 VOID,
349                 BOOL,
350                 INT,
351                 FLOAT,
352                 VECTOR,
353                 MATRIX,
354                 ARRAY
355         };
356
357         Kind kind;
358         unsigned size;
359         std::string base;
360
361         TypeDeclaration *base_type;
362
363         BasicTypeDeclaration();
364         BasicTypeDeclaration(const BasicTypeDeclaration &);
365
366         virtual BasicTypeDeclaration *clone() const { return new BasicTypeDeclaration(*this); }
367         virtual void visit(NodeVisitor &);
368 };
369
370 struct ImageTypeDeclaration: TypeDeclaration
371 {
372         enum Dimensions
373         {
374                 ONE = 1,
375                 TWO,
376                 THREE,
377                 CUBE
378         };
379
380         Dimensions dimensions;
381         bool array;
382         bool sampled;
383         bool shadow;
384         std::string base;
385
386         TypeDeclaration *base_type;
387
388         ImageTypeDeclaration();
389
390         virtual ImageTypeDeclaration *clone() const { return new ImageTypeDeclaration(*this); }
391         virtual void visit(NodeVisitor &);
392 };
393
394 struct StructDeclaration: TypeDeclaration
395 {
396         Block members;
397
398         InterfaceBlock *interface_block;
399
400         StructDeclaration();
401         StructDeclaration(const StructDeclaration &);
402         ~StructDeclaration();
403
404         virtual StructDeclaration *clone() const { return new StructDeclaration(*this); }
405         virtual void visit(NodeVisitor &);
406 };
407
408 struct VariableDeclaration: Statement
409 {
410         NodePtr<Layout> layout;
411         bool constant;
412         std::string sampling;
413         std::string interpolation;
414         std::string interface;
415         std::string precision;
416         std::string type;
417         std::string name;
418         bool array;
419         NodePtr<Expression> array_size;
420         NodePtr<Expression> init_expression;
421
422         TypeDeclaration *type_declaration;
423         VariableDeclaration *linked_declaration;
424
425         VariableDeclaration();
426         VariableDeclaration(const VariableDeclaration &);
427         ~VariableDeclaration();
428
429         virtual VariableDeclaration *clone() const { return new VariableDeclaration(*this); }
430         virtual void visit(NodeVisitor &);
431 };
432
433 struct InterfaceBlock: Statement
434 {
435         NodePtr<Layout> layout;
436         std::string interface;
437         std::string block_name;
438         NodePtr<Block> members;
439         std::string instance_name;
440         bool array;
441
442         /* An interface block's ultimate base type is always a struct.  The
443         immediate type may be either that same struct or an array of it. */
444         TypeDeclaration *type_declaration;
445         StructDeclaration *struct_declaration;
446         InterfaceBlock *linked_block;
447
448         InterfaceBlock();
449         InterfaceBlock(const InterfaceBlock &);
450         ~InterfaceBlock();
451
452         virtual InterfaceBlock *clone() const { return new InterfaceBlock(*this); }
453         virtual void visit(NodeVisitor &);
454 };
455
456 struct FunctionDeclaration: Statement
457 {
458         std::string return_type;
459         std::string name;
460         NodeArray<VariableDeclaration> parameters;
461         bool virtua;
462         bool overrd;
463         Block body;
464
465         std::string signature;
466         FunctionDeclaration *definition;
467         TypeDeclaration *return_type_declaration;
468
469         FunctionDeclaration();
470         FunctionDeclaration(const FunctionDeclaration &);
471
472         virtual FunctionDeclaration *clone() const { return new FunctionDeclaration(*this); }
473         virtual void visit(NodeVisitor &);
474 };
475
476 struct Conditional: Statement
477 {
478         NodePtr<Expression> condition;
479         Block body;
480         Block else_body;
481
482         virtual Conditional *clone() const { return new Conditional(*this); }
483         virtual void visit(NodeVisitor &);
484 };
485
486 struct Iteration: Statement
487 {
488         NodePtr<Statement> init_statement;
489         NodePtr<Expression> condition;
490         NodePtr<Expression> loop_expression;
491         Block body;
492
493         virtual Iteration *clone() const { return new Iteration(*this); }
494         virtual void visit(NodeVisitor &);
495 };
496
497 struct Passthrough: Statement
498 {
499         NodePtr<Expression> subscript;
500
501         virtual Passthrough *clone() const { return new Passthrough(*this); }
502         virtual void visit(NodeVisitor &);
503 };
504
505 struct Return: Statement
506 {
507         NodePtr<Expression> expression;
508
509         virtual Return *clone() const { return new Return(*this); }
510         virtual void visit(NodeVisitor &);
511 };
512
513 struct Jump: Statement
514 {
515         std::string keyword;
516
517         virtual Jump *clone() const { return new Jump(*this); }
518         virtual void visit(NodeVisitor &);
519 };
520
521 struct Stage
522 {
523         enum Type
524         {
525                 SHARED,
526                 VERTEX,
527                 GEOMETRY,
528                 FRAGMENT
529         };
530
531         Type type;
532         Stage *previous;
533         Block content;
534         std::map<std::string, TypeDeclaration *> types;
535         std::map<std::string, InterfaceBlock *> interface_blocks;
536         std::map<std::string, FunctionDeclaration *> functions;
537         std::map<std::string, unsigned> locations;
538         std::map<std::string, unsigned> texture_bindings;
539         std::map<std::string, unsigned> uniform_block_bindings;
540         Features required_features;
541         std::vector<Diagnostic> diagnostics;
542
543         Stage(Type);
544
545         static const char *get_stage_name(Type);
546 };
547
548 struct Module
549 {
550         SourceMap source_map;
551         Stage shared;
552         std::list<Stage> stages;
553
554         Module();
555 };
556
557 std::string get_unused_variable_name(const Block &, const std::string &);
558
559 bool is_same_type(const TypeDeclaration &, const TypeDeclaration &);
560 int get_layout_value(const Layout &, const std::string &, int = -1);
561 void add_to_chain(Assignment::Target &, Assignment::Target::ChainType, unsigned);
562
563 } // namespace SL
564 } // namespace GL
565 } // namespace Msp
566
567 #pragma pop_macro("interface")
568
569 #endif