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