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