]> git.tdb.fi Git - libs/gl.git/blob - source/programsyntax.h
Automatically backport shaders to earlier GLSL versions if necessary
[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         std::string interface;
186
187         virtual Layout *clone() const { return new Layout(*this); }
188         virtual void visit(NodeVisitor &);
189 };
190
191 struct StructDeclaration: Node
192 {
193         std::string name;
194         Block members;
195
196         StructDeclaration();
197
198         virtual StructDeclaration *clone() const { return new StructDeclaration(*this); }
199         virtual void visit(NodeVisitor &);
200 };
201
202 struct VariableDeclaration: Node
203 {
204         bool constant;
205         std::string sampling;
206         std::string interface;
207         std::string type;
208         StructDeclaration *type_declaration;
209         std::string name;
210         bool array;
211         NodePtr<Expression> array_size;
212         NodePtr<Expression> init_expression;
213         VariableDeclaration *linked_declaration;
214
215         VariableDeclaration();
216
217         virtual VariableDeclaration *clone() const { return new VariableDeclaration(*this); }
218         virtual void visit(NodeVisitor &);
219 };
220
221 struct InterfaceBlock: Node
222 {
223         std::string interface;
224         std::string name;
225         Block members;
226         std::string instance_name;
227         bool array;
228
229         InterfaceBlock();
230
231         virtual InterfaceBlock *clone() const { return new InterfaceBlock(*this); }
232         virtual void visit(NodeVisitor &);
233 };
234
235 struct FunctionDeclaration: Node
236 {
237         std::string return_type;
238         std::string name;
239         std::vector<NodePtr<VariableDeclaration> > parameters;
240         FunctionDeclaration *definition;
241         Block body;
242
243         FunctionDeclaration();
244         FunctionDeclaration(const FunctionDeclaration &);
245
246         virtual FunctionDeclaration *clone() const { return new FunctionDeclaration(*this); }
247         virtual void visit(NodeVisitor &);
248 };
249
250 struct Conditional: Node
251 {
252         NodePtr<Expression> condition;
253         Block body;
254         Block else_body;
255
256         virtual Conditional *clone() const { return new Conditional(*this); }
257         virtual void visit(NodeVisitor &);
258 };
259
260 struct Iteration: Node
261 {
262         NodePtr<Node> init_statement;
263         NodePtr<Expression> condition;
264         NodePtr<Expression> loop_expression;
265         Block body;
266
267         virtual Iteration *clone() const { return new Iteration(*this); }
268         virtual void visit(NodeVisitor &);
269 };
270
271 struct Passthrough: Node
272 {
273         NodePtr<Expression> subscript;
274
275         virtual Passthrough *clone() const { return new Passthrough(*this); }
276         virtual void visit(NodeVisitor &);
277 };
278
279 struct Return: Node
280 {
281         NodePtr<Expression> expression;
282
283         virtual Return *clone() const { return new Return(*this); }
284         virtual void visit(NodeVisitor &);
285 };
286
287 struct NodeVisitor
288 {
289         virtual ~NodeVisitor() { }
290
291         virtual void visit(Block &) { }
292         virtual void visit(Literal &) { }
293         virtual void visit(ParenthesizedExpression &) { }
294         virtual void visit(VariableReference &) { }
295         virtual void visit(MemberAccess &) { }
296         virtual void visit(UnaryExpression &) { }
297         virtual void visit(BinaryExpression &) { }
298         virtual void visit(Assignment &);
299         virtual void visit(FunctionCall &) { }
300         virtual void visit(ExpressionStatement &) { }
301         virtual void visit(Import &) { }
302         virtual void visit(Layout &) { }
303         virtual void visit(StructDeclaration &) { }
304         virtual void visit(VariableDeclaration &) { }
305         virtual void visit(InterfaceBlock &) { }
306         virtual void visit(FunctionDeclaration &) { }
307         virtual void visit(Conditional &) { }
308         virtual void visit(Iteration &) { }
309         virtual void visit(Passthrough &) { }
310         virtual void visit(Return &) { }
311 };
312
313 struct TraversingVisitor: NodeVisitor
314 {
315         virtual void visit(Block &);
316         virtual void visit(ParenthesizedExpression &);
317         virtual void visit(MemberAccess &);
318         virtual void visit(UnaryExpression &);
319         virtual void visit(BinaryExpression &);
320         virtual void visit(FunctionCall &);
321         virtual void visit(ExpressionStatement &);
322         virtual void visit(StructDeclaration &);
323         virtual void visit(VariableDeclaration &);
324         virtual void visit(InterfaceBlock &);
325         virtual void visit(FunctionDeclaration &);
326         virtual void visit(Conditional &);
327         virtual void visit(Iteration &);
328         virtual void visit(Passthrough &);
329         virtual void visit(Return &);
330 };
331
332 enum StageType
333 {
334         SHARED,
335         VERTEX,
336         GEOMETRY,
337         FRAGMENT
338 };
339
340 struct Stage
341 {
342         StageType type;
343         Stage *previous;
344         ProgramSyntax::Block content;
345         std::map<std::string, VariableDeclaration *> in_variables;
346         std::map<std::string, VariableDeclaration *> out_variables;
347         Version required_version;
348
349         Stage(StageType);
350 };
351
352 struct Module
353 {
354         Stage shared;
355         std::list<Stage> stages;
356
357         Module();
358 };
359
360 } // namespace ProgramSyntax
361 } // namespace GL
362 } // namespace Msp
363
364 #endif