]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/syntax.h
Assign a result type to all expressions
[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/refptr.h>
10 #include <msp/core/variant.h>
11 #include "features.h"
12 #include "glsl_error.h"
13 #include "sourcemap.h"
14
15 #pragma push_macro("interface")
16 #undef interface
17
18 namespace Msp {
19 namespace GL {
20 namespace SL {
21
22 struct Operator
23 {
24         enum Type
25         {
26                 NO_OPERATOR,
27                 BINARY,
28                 PREFIX,
29                 POSTFIX
30         };
31
32         enum Associativity
33         {
34                 LEFT_TO_RIGHT,
35                 RIGHT_TO_LEFT,
36                 ASSOCIATIVE
37         };
38
39         char token[4];
40         unsigned precedence;
41         Type type;
42         Associativity assoc;
43
44         static const Operator operators[];
45
46         static const Operator &get_operator(const std::string &, Type);
47 };
48
49 enum
50 {
51         BUILTIN_SOURCE = -1,
52         GENERATED_SOURCE = 0
53 };
54
55 struct NodeVisitor;
56
57 struct Node
58 {
59         int source;
60         unsigned line;
61
62         Node(): source(GENERATED_SOURCE), line(1) { }
63         Node(const Node &n): source(n.source), line(n.line) { }
64 private:
65         Node &operator=(const Node &);
66 public:
67         virtual ~Node() { }
68
69         virtual Node *clone() const = 0;
70         virtual void visit(NodeVisitor &) = 0;
71 };
72
73 template<typename T>
74 class NodePtr: public RefPtr<T>
75 {
76 public:
77         NodePtr() { }
78         NodePtr(T *p): RefPtr<T>(p) { }
79         NodePtr(const NodePtr &p): RefPtr<T>(p ? p->clone() : 0) { }
80         NodePtr &operator=(const NodePtr &p) { RefPtr<T>::operator=(p); return *this; }
81
82         template<typename U>
83         NodePtr(const RefPtr<U> &p): RefPtr<T>(p) { }
84
85         template<typename U>
86         NodePtr(const NodePtr<U> &p): RefPtr<T>(p ? p->clone() : 0) { }
87 };
88
89 template<typename C>
90 class NodeContainer: public C
91 {
92 public:
93         NodeContainer() { }
94         NodeContainer(const NodeContainer &);
95 };
96
97 template<typename T>
98 class NodeList: public NodeContainer<std::list<RefPtr<T> > >
99 { };
100
101 template<typename T>
102 class NodeArray: public NodeContainer<std::vector<RefPtr<T> > >
103 { };
104
105 struct TypeDeclaration;
106 struct VariableDeclaration;
107 struct InterfaceBlock;
108 struct FunctionDeclaration;
109
110 struct Statement: Node
111 {
112         virtual Statement *clone() const = 0;
113 };
114
115 struct Block: Node
116 {
117         NodeList<Statement> body;
118         bool use_braces;
119
120         std::map<std::string, VariableDeclaration *> variables;
121         Block *parent;
122
123         Block();
124         Block(const Block &);
125
126         virtual Block *clone() const { return new Block(*this); }
127         virtual void visit(NodeVisitor &);
128 };
129
130 struct Expression: Node
131 {
132         const Operator *oper;
133
134         TypeDeclaration *type;
135         bool lvalue;
136
137         Expression();
138
139         virtual Expression *clone() const = 0;
140 };
141
142 struct Literal: Expression
143 {
144         std::string token;
145         Variant value;
146
147         virtual Literal *clone() const { return new Literal(*this); }
148         virtual void visit(NodeVisitor &);
149 };
150
151 struct ParenthesizedExpression: Expression
152 {
153         NodePtr<Expression> expression;
154
155         virtual ParenthesizedExpression *clone() const { return new ParenthesizedExpression(*this); }
156         virtual void visit(NodeVisitor &);
157 };
158
159 struct VariableReference: Expression
160 {
161         std::string name;
162
163         VariableDeclaration *declaration;
164
165         VariableReference();
166         VariableReference(const VariableReference &);
167
168         virtual VariableReference *clone() const { return new VariableReference(*this); }
169         virtual void visit(NodeVisitor &);
170 };
171
172 struct InterfaceBlockReference: Expression
173 {
174         std::string name;
175
176         InterfaceBlock *declaration;
177
178         InterfaceBlockReference();
179         InterfaceBlockReference(const InterfaceBlockReference &);
180
181         virtual InterfaceBlockReference *clone() const { return new InterfaceBlockReference(*this); }
182         virtual void visit(NodeVisitor &);
183 };
184
185 struct MemberAccess: Expression
186 {
187         NodePtr<Expression> left;
188         std::string member;
189
190         VariableDeclaration *declaration;
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 UnaryExpression: Expression
200 {
201         NodePtr<Expression> expression;
202
203         virtual UnaryExpression *clone() const { return new UnaryExpression(*this); }
204         virtual void visit(NodeVisitor &);
205 };
206
207 struct BinaryExpression: Expression
208 {
209         NodePtr<Expression> left;
210         NodePtr<Expression> right;
211
212         virtual BinaryExpression *clone() const { return new BinaryExpression(*this); }
213         virtual void visit(NodeVisitor &);
214 };
215
216 struct Assignment: BinaryExpression
217 {
218         bool self_referencing;
219
220         VariableDeclaration *target_declaration;
221
222         Assignment();
223         Assignment(const Assignment &);
224
225         virtual Assignment *clone() const { return new Assignment(*this); }
226         virtual void visit(NodeVisitor &);
227 };
228
229 struct FunctionCall: Expression
230 {
231         std::string name;
232         bool constructor;
233         NodeArray<Expression> arguments;
234
235         FunctionDeclaration *declaration;
236
237         FunctionCall();
238         FunctionCall(const FunctionCall &);
239
240         virtual FunctionCall *clone() const { return new FunctionCall(*this); }
241         virtual void visit(NodeVisitor &);
242 };
243
244 struct ExpressionStatement: Statement
245 {
246         NodePtr<Expression> expression;
247
248         virtual ExpressionStatement *clone() const { return new ExpressionStatement(*this); }
249         virtual void visit(NodeVisitor &);
250 };
251
252 struct Import: Statement
253 {
254         std::string module;
255
256         virtual Import *clone() const { return new Import(*this); }
257         virtual void visit(NodeVisitor &);
258 };
259
260 struct Precision: Statement
261 {
262         std::string precision;
263         std::string type;
264
265         virtual Precision *clone() const { return new Precision(*this); }
266         virtual void visit(NodeVisitor &);
267 };
268
269 struct Layout: Node
270 {
271         struct Qualifier
272         {
273                 std::string name;
274                 bool has_value;
275                 int value;
276         };
277
278         std::vector<Qualifier> qualifiers;
279
280         virtual Layout *clone() const { return new Layout(*this); }
281         virtual void visit(NodeVisitor &);
282 };
283
284 struct InterfaceLayout: Statement
285 {
286         std::string interface;
287         Layout layout;
288
289         virtual InterfaceLayout *clone() const { return new InterfaceLayout(*this); }
290         virtual void visit(NodeVisitor &);
291 };
292
293 struct TypeDeclaration: Statement
294 {
295         std::string name;
296
297         virtual TypeDeclaration *clone() const = 0;
298 };
299
300 struct BasicTypeDeclaration: TypeDeclaration
301 {
302         enum Kind
303         {
304                 ALIAS,
305                 VOID,
306                 BOOL,
307                 INT,
308                 FLOAT,
309                 VECTOR,
310                 MATRIX,
311                 ARRAY
312         };
313
314         Kind kind;
315         unsigned size;
316         std::string base;
317
318         TypeDeclaration *base_type;
319
320         BasicTypeDeclaration();
321         BasicTypeDeclaration(const BasicTypeDeclaration &);
322
323         virtual BasicTypeDeclaration *clone() const { return new BasicTypeDeclaration(*this); }
324         virtual void visit(NodeVisitor &);
325 };
326
327 struct ImageTypeDeclaration: TypeDeclaration
328 {
329         enum Dimensions
330         {
331                 ONE = 1,
332                 TWO,
333                 THREE,
334                 CUBE
335         };
336
337         Dimensions dimensions;
338         bool array;
339         bool sampled;
340         bool shadow;
341         std::string base;
342
343         TypeDeclaration *base_type;
344
345         ImageTypeDeclaration();
346
347         virtual ImageTypeDeclaration *clone() const { return new ImageTypeDeclaration(*this); }
348         virtual void visit(NodeVisitor &);
349 };
350
351 struct StructDeclaration: TypeDeclaration
352 {
353         Block members;
354
355         StructDeclaration();
356
357         virtual StructDeclaration *clone() const { return new StructDeclaration(*this); }
358         virtual void visit(NodeVisitor &);
359 };
360
361 struct VariableDeclaration: Statement
362 {
363         NodePtr<Layout> layout;
364         bool constant;
365         std::string sampling;
366         std::string interpolation;
367         std::string interface;
368         std::string precision;
369         std::string type;
370         std::string name;
371         bool array;
372         NodePtr<Expression> array_size;
373         NodePtr<Expression> init_expression;
374
375         TypeDeclaration *type_declaration;
376         VariableDeclaration *linked_declaration;
377
378         VariableDeclaration();
379         VariableDeclaration(const VariableDeclaration &);
380         ~VariableDeclaration();
381
382         virtual VariableDeclaration *clone() const { return new VariableDeclaration(*this); }
383         virtual void visit(NodeVisitor &);
384 };
385
386 struct InterfaceBlock: Statement
387 {
388         std::string interface;
389         std::string name;
390         Block members;
391         std::string instance_name;
392         bool array;
393
394         InterfaceBlock *linked_block;
395
396         InterfaceBlock();
397         InterfaceBlock(const InterfaceBlock &);
398         ~InterfaceBlock();
399
400         virtual InterfaceBlock *clone() const { return new InterfaceBlock(*this); }
401         virtual void visit(NodeVisitor &);
402 };
403
404 struct FunctionDeclaration: Statement
405 {
406         std::string return_type;
407         std::string name;
408         NodeArray<VariableDeclaration> parameters;
409         Block body;
410
411         FunctionDeclaration *definition;
412         TypeDeclaration *return_type_declaration;
413
414         FunctionDeclaration();
415         FunctionDeclaration(const FunctionDeclaration &);
416
417         virtual FunctionDeclaration *clone() const { return new FunctionDeclaration(*this); }
418         virtual void visit(NodeVisitor &);
419 };
420
421 struct Conditional: Statement
422 {
423         NodePtr<Expression> condition;
424         Block body;
425         Block else_body;
426
427         virtual Conditional *clone() const { return new Conditional(*this); }
428         virtual void visit(NodeVisitor &);
429 };
430
431 struct Iteration: Statement
432 {
433         NodePtr<Statement> init_statement;
434         NodePtr<Expression> condition;
435         NodePtr<Expression> loop_expression;
436         Block body;
437
438         virtual Iteration *clone() const { return new Iteration(*this); }
439         virtual void visit(NodeVisitor &);
440 };
441
442 struct Passthrough: Statement
443 {
444         NodePtr<Expression> subscript;
445
446         virtual Passthrough *clone() const { return new Passthrough(*this); }
447         virtual void visit(NodeVisitor &);
448 };
449
450 struct Return: Statement
451 {
452         NodePtr<Expression> expression;
453
454         virtual Return *clone() const { return new Return(*this); }
455         virtual void visit(NodeVisitor &);
456 };
457
458 struct Jump: Statement
459 {
460         std::string keyword;
461
462         virtual Jump *clone() const { return new Jump(*this); }
463         virtual void visit(NodeVisitor &);
464 };
465
466 struct Stage
467 {
468         enum Type
469         {
470                 SHARED,
471                 VERTEX,
472                 GEOMETRY,
473                 FRAGMENT
474         };
475
476         Type type;
477         Stage *previous;
478         Block content;
479         std::map<std::string, TypeDeclaration *> types;
480         std::map<std::string, InterfaceBlock *> interface_blocks;
481         std::map<std::string, FunctionDeclaration *> functions;
482         std::map<std::string, unsigned> locations;
483         Features required_features;
484         std::vector<Diagnostic> diagnostics;
485
486         Stage(Type);
487
488         static const char *get_stage_name(Type);
489 };
490
491 struct Module
492 {
493         SourceMap source_map;
494         Stage shared;
495         std::list<Stage> stages;
496
497         Module();
498 };
499
500 } // namespace SL
501 } // namespace GL
502 } // namespace Msp
503
504 #pragma pop_macro("interface")
505
506 #endif