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