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