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