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