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