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