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