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