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