]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/syntax.h
Store a pointer to operator info rather than the token in expressions
[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         static const Operator &get_operator(const std::string &, Type);
44 };
45
46 enum
47 {
48         BUILTIN_SOURCE = -1,
49         GENERATED_SOURCE = 0
50 };
51
52 struct NodeVisitor;
53
54 struct Node
55 {
56 protected:
57         Node() { }
58         Node(const Node &) { }
59 private:
60         Node &operator=(const Node &);
61 public:
62         virtual ~Node() { }
63
64         virtual Node *clone() const = 0;
65         virtual void visit(NodeVisitor &) = 0;
66 };
67
68 template<typename T>
69 class NodePtr: public RefPtr<T>
70 {
71 public:
72         NodePtr() { }
73         NodePtr(T *p): RefPtr<T>(p) { }
74         NodePtr(const NodePtr &p): RefPtr<T>(p ? p->clone() : 0) { }
75         NodePtr &operator=(const NodePtr &p) { RefPtr<T>::operator=(p); return *this; }
76
77         template<typename U>
78         NodePtr(const RefPtr<U> &p): RefPtr<T>(p) { }
79
80         template<typename U>
81         NodePtr(const NodePtr<U> &p): RefPtr<T>(p ? p->clone() : 0) { }
82 };
83
84 template<typename C>
85 class NodeContainer: public C
86 {
87 public:
88         NodeContainer() { }
89         NodeContainer(const NodeContainer &);
90 };
91
92 template<typename T>
93 class NodeList: public NodeContainer<std::list<RefPtr<T> > >
94 { };
95
96 template<typename T>
97 class NodeArray: public NodeContainer<std::vector<RefPtr<T> > >
98 { };
99
100 struct StructDeclaration;
101 struct VariableDeclaration;
102 struct InterfaceBlock;
103 struct FunctionDeclaration;
104
105 struct Statement: Node
106 {
107         int source;
108         unsigned line;
109
110         Statement();
111
112         virtual Statement *clone() const = 0;
113 };
114
115 struct Block: Node
116 {
117         NodeList<Statement> body;
118         bool use_braces;
119
120         std::map<std::string, VariableDeclaration *> variables;
121         Block *parent;
122
123         Block();
124         Block(const Block &);
125
126         virtual Block *clone() const { return new Block(*this); }
127         virtual void visit(NodeVisitor &);
128 };
129
130 struct Expression: Node
131 {
132         const Operator *oper;
133
134         Expression();
135
136         virtual Expression *clone() const = 0;
137 };
138
139 struct Literal: Expression
140 {
141         std::string token;
142
143         virtual Literal *clone() const { return new Literal(*this); }
144         virtual void visit(NodeVisitor &);
145 };
146
147 struct ParenthesizedExpression: Expression
148 {
149         NodePtr<Expression> expression;
150
151         virtual ParenthesizedExpression *clone() const { return new ParenthesizedExpression(*this); }
152         virtual void visit(NodeVisitor &);
153 };
154
155 struct VariableReference: Expression
156 {
157         std::string name;
158
159         VariableDeclaration *declaration;
160
161         VariableReference();
162         VariableReference(const VariableReference &);
163
164         virtual VariableReference *clone() const { return new VariableReference(*this); }
165         virtual void visit(NodeVisitor &);
166 };
167
168 struct InterfaceBlockReference: Expression
169 {
170         std::string name;
171
172         InterfaceBlock *declaration;
173
174         InterfaceBlockReference();
175         InterfaceBlockReference(const InterfaceBlockReference &);
176
177         virtual InterfaceBlockReference *clone() const { return new InterfaceBlockReference(*this); }
178         virtual void visit(NodeVisitor &);
179 };
180
181 struct MemberAccess: Expression
182 {
183         NodePtr<Expression> left;
184         std::string member;
185
186         VariableDeclaration *declaration;
187
188         MemberAccess();
189         MemberAccess(const MemberAccess &);
190
191         virtual MemberAccess *clone() const { return new MemberAccess(*this); }
192         virtual void visit(NodeVisitor &);
193 };
194
195 struct UnaryExpression: Expression
196 {
197         NodePtr<Expression> expression;
198
199         virtual UnaryExpression *clone() const { return new UnaryExpression(*this); }
200         virtual void visit(NodeVisitor &);
201 };
202
203 struct BinaryExpression: Expression
204 {
205         NodePtr<Expression> left;
206         NodePtr<Expression> right;
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