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