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