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