]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/syntax.h
Store actual values for literals
[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 StructDeclaration;
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         Expression();
135
136         virtual Expression *clone() const = 0;
137 };
138
139 struct Literal: Expression
140 {
141         std::string token;
142         Variant value;
143
144         virtual Literal *clone() const { return new Literal(*this); }
145         virtual void visit(NodeVisitor &);
146 };
147
148 struct ParenthesizedExpression: Expression
149 {
150         NodePtr<Expression> expression;
151
152         virtual ParenthesizedExpression *clone() const { return new ParenthesizedExpression(*this); }
153         virtual void visit(NodeVisitor &);
154 };
155
156 struct VariableReference: Expression
157 {
158         std::string name;
159
160         VariableDeclaration *declaration;
161
162         VariableReference();
163         VariableReference(const VariableReference &);
164
165         virtual VariableReference *clone() const { return new VariableReference(*this); }
166         virtual void visit(NodeVisitor &);
167 };
168
169 struct InterfaceBlockReference: Expression
170 {
171         std::string name;
172
173         InterfaceBlock *declaration;
174
175         InterfaceBlockReference();
176         InterfaceBlockReference(const InterfaceBlockReference &);
177
178         virtual InterfaceBlockReference *clone() const { return new InterfaceBlockReference(*this); }
179         virtual void visit(NodeVisitor &);
180 };
181
182 struct MemberAccess: Expression
183 {
184         NodePtr<Expression> left;
185         std::string member;
186
187         VariableDeclaration *declaration;
188
189         MemberAccess();
190         MemberAccess(const MemberAccess &);
191
192         virtual MemberAccess *clone() const { return new MemberAccess(*this); }
193         virtual void visit(NodeVisitor &);
194 };
195
196 struct UnaryExpression: Expression
197 {
198         NodePtr<Expression> expression;
199
200         virtual UnaryExpression *clone() const { return new UnaryExpression(*this); }
201         virtual void visit(NodeVisitor &);
202 };
203
204 struct BinaryExpression: Expression
205 {
206         NodePtr<Expression> left;
207         NodePtr<Expression> right;
208
209         virtual BinaryExpression *clone() const { return new BinaryExpression(*this); }
210         virtual void visit(NodeVisitor &);
211 };
212
213 struct Assignment: BinaryExpression
214 {
215         bool self_referencing;
216
217         VariableDeclaration *target_declaration;
218
219         Assignment();
220         Assignment(const Assignment &);
221
222         virtual Assignment *clone() const { return new Assignment(*this); }
223         virtual void visit(NodeVisitor &);
224 };
225
226 struct FunctionCall: Expression
227 {
228         std::string name;
229         bool constructor;
230         NodeArray<Expression> arguments;
231
232         FunctionDeclaration *declaration;
233
234         FunctionCall();
235         FunctionCall(const FunctionCall &);
236
237         virtual FunctionCall *clone() const { return new FunctionCall(*this); }
238         virtual void visit(NodeVisitor &);
239 };
240
241 struct ExpressionStatement: Statement
242 {
243         NodePtr<Expression> expression;
244
245         virtual ExpressionStatement *clone() const { return new ExpressionStatement(*this); }
246         virtual void visit(NodeVisitor &);
247 };
248
249 struct Import: Statement
250 {
251         std::string module;
252
253         virtual Import *clone() const { return new Import(*this); }
254         virtual void visit(NodeVisitor &);
255 };
256
257 struct Precision: Statement
258 {
259         std::string precision;
260         std::string type;
261
262         virtual Precision *clone() const { return new Precision(*this); }
263         virtual void visit(NodeVisitor &);
264 };
265
266 struct Layout: Node
267 {
268         struct Qualifier
269         {
270                 std::string name;
271                 bool has_value;
272                 int value;
273         };
274
275         std::vector<Qualifier> qualifiers;
276
277         virtual Layout *clone() const { return new Layout(*this); }
278         virtual void visit(NodeVisitor &);
279 };
280
281 struct InterfaceLayout: Statement
282 {
283         std::string interface;
284         Layout layout;
285
286         virtual InterfaceLayout *clone() const { return new InterfaceLayout(*this); }
287         virtual void visit(NodeVisitor &);
288 };
289
290 struct TypeDeclaration: Statement
291 {
292         std::string name;
293
294         virtual TypeDeclaration *clone() const = 0;
295 };
296
297 struct BasicTypeDeclaration: TypeDeclaration
298 {
299         enum Kind
300         {
301                 ALIAS,
302                 VOID,
303                 BOOL,
304                 INT,
305                 FLOAT,
306                 VECTOR,
307                 MATRIX,
308                 ARRAY
309         };
310
311         Kind kind;
312         unsigned size;
313         std::string base;
314
315         TypeDeclaration *base_type;
316
317         BasicTypeDeclaration();
318         BasicTypeDeclaration(const BasicTypeDeclaration &);
319
320         virtual BasicTypeDeclaration *clone() const { return new BasicTypeDeclaration(*this); }
321         virtual void visit(NodeVisitor &);
322 };
323
324 struct ImageTypeDeclaration: TypeDeclaration
325 {
326         enum Dimensions
327         {
328                 ONE = 1,
329                 TWO,
330                 THREE,
331                 CUBE
332         };
333
334         Dimensions dimensions;
335         bool array;
336         bool sampled;
337         bool shadow;
338         std::string base;
339
340         TypeDeclaration *base_type;
341
342         ImageTypeDeclaration();
343
344         virtual ImageTypeDeclaration *clone() const { return new ImageTypeDeclaration(*this); }
345         virtual void visit(NodeVisitor &);
346 };
347
348 struct StructDeclaration: TypeDeclaration
349 {
350         Block members;
351
352         StructDeclaration();
353
354         virtual StructDeclaration *clone() const { return new StructDeclaration(*this); }
355         virtual void visit(NodeVisitor &);
356 };
357
358 struct VariableDeclaration: Statement
359 {
360         NodePtr<Layout> layout;
361         bool constant;
362         std::string sampling;
363         std::string interpolation;
364         std::string interface;
365         std::string precision;
366         std::string type;
367         std::string name;
368         bool array;
369         NodePtr<Expression> array_size;
370         NodePtr<Expression> init_expression;
371
372         TypeDeclaration *type_declaration;
373         VariableDeclaration *linked_declaration;
374
375         VariableDeclaration();
376         VariableDeclaration(const VariableDeclaration &);
377         ~VariableDeclaration();
378
379         virtual VariableDeclaration *clone() const { return new VariableDeclaration(*this); }
380         virtual void visit(NodeVisitor &);
381 };
382
383 struct InterfaceBlock: Statement
384 {
385         std::string interface;
386         std::string name;
387         Block members;
388         std::string instance_name;
389         bool array;
390
391         InterfaceBlock *linked_block;
392
393         InterfaceBlock();
394         InterfaceBlock(const InterfaceBlock &);
395         ~InterfaceBlock();
396
397         virtual InterfaceBlock *clone() const { return new InterfaceBlock(*this); }
398         virtual void visit(NodeVisitor &);
399 };
400
401 struct FunctionDeclaration: Statement
402 {
403         std::string return_type;
404         std::string name;
405         NodeArray<VariableDeclaration> parameters;
406         Block body;
407
408         FunctionDeclaration *definition;
409         TypeDeclaration *return_type_declaration;
410
411         FunctionDeclaration();
412         FunctionDeclaration(const FunctionDeclaration &);
413
414         virtual FunctionDeclaration *clone() const { return new FunctionDeclaration(*this); }
415         virtual void visit(NodeVisitor &);
416 };
417
418 struct Conditional: Statement
419 {
420         NodePtr<Expression> condition;
421         Block body;
422         Block else_body;
423
424         virtual Conditional *clone() const { return new Conditional(*this); }
425         virtual void visit(NodeVisitor &);
426 };
427
428 struct Iteration: Statement
429 {
430         NodePtr<Statement> init_statement;
431         NodePtr<Expression> condition;
432         NodePtr<Expression> loop_expression;
433         Block body;
434
435         virtual Iteration *clone() const { return new Iteration(*this); }
436         virtual void visit(NodeVisitor &);
437 };
438
439 struct Passthrough: Statement
440 {
441         NodePtr<Expression> subscript;
442
443         virtual Passthrough *clone() const { return new Passthrough(*this); }
444         virtual void visit(NodeVisitor &);
445 };
446
447 struct Return: Statement
448 {
449         NodePtr<Expression> expression;
450
451         virtual Return *clone() const { return new Return(*this); }
452         virtual void visit(NodeVisitor &);
453 };
454
455 struct Jump: Statement
456 {
457         std::string keyword;
458
459         virtual Jump *clone() const { return new Jump(*this); }
460         virtual void visit(NodeVisitor &);
461 };
462
463 struct Stage
464 {
465         enum Type
466         {
467                 SHARED,
468                 VERTEX,
469                 GEOMETRY,
470                 FRAGMENT
471         };
472
473         Type type;
474         Stage *previous;
475         Block content;
476         std::map<std::string, TypeDeclaration *> types;
477         std::map<std::string, InterfaceBlock *> interface_blocks;
478         std::map<std::string, FunctionDeclaration *> functions;
479         std::map<std::string, unsigned> locations;
480         Features required_features;
481         std::vector<Diagnostic> diagnostics;
482
483         Stage(Type);
484
485         static const char *get_stage_name(Type);
486 };
487
488 struct Module
489 {
490         SourceMap source_map;
491         Stage shared;
492         std::list<Stage> stages;
493
494         Module();
495 };
496
497 } // namespace SL
498 } // namespace GL
499 } // namespace Msp
500
501 #pragma pop_macro("interface")
502
503 #endif