]> git.tdb.fi Git - libs/gl.git/blob - source/programsyntax.h
Add a module for builtin interface 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
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         bool assignment;
130
131         BinaryExpression();
132
133         virtual BinaryExpression *clone() const { return new BinaryExpression(*this); }
134         virtual void visit(NodeVisitor &);
135 };
136
137 struct FunctionCall: Expression
138 {
139         std::string name;
140         bool constructor;
141         std::vector<NodePtr<Expression> > arguments;
142
143         FunctionCall();
144
145         virtual FunctionCall *clone() const { return new FunctionCall(*this); }
146         virtual void visit(NodeVisitor &);
147 };
148
149 struct ExpressionStatement: Node
150 {
151         NodePtr<Expression> expression;
152
153         virtual ExpressionStatement *clone() const { return new ExpressionStatement(*this); }
154         virtual void visit(NodeVisitor &);
155 };
156
157 struct Layout: Node
158 {
159         struct Qualifier
160         {
161                 std::string identifier;
162                 std::string value;
163         };
164
165         std::vector<Qualifier> qualifiers;
166         std::string interface;
167
168         virtual Layout *clone() const { return new Layout(*this); }
169         virtual void visit(NodeVisitor &);
170 };
171
172 struct StructDeclaration: Node
173 {
174         std::string name;
175         Block members;
176
177         StructDeclaration();
178
179         virtual StructDeclaration *clone() const { return new StructDeclaration(*this); }
180         virtual void visit(NodeVisitor &);
181 };
182
183 struct VariableDeclaration: Node
184 {
185         bool constant;
186         std::string sampling;
187         std::string interface;
188         std::string type;
189         StructDeclaration *type_declaration;
190         std::string name;
191         bool array;
192         NodePtr<Expression> array_size;
193         NodePtr<Expression> init_expression;
194         VariableDeclaration *linked_declaration;
195
196         VariableDeclaration();
197
198         virtual VariableDeclaration *clone() const { return new VariableDeclaration(*this); }
199         virtual void visit(NodeVisitor &);
200 };
201
202 struct InterfaceBlock: Node
203 {
204         std::string interface;
205         std::string name;
206         Block members;
207         std::string instance_name;
208         bool array;
209
210         InterfaceBlock();
211
212         virtual InterfaceBlock *clone() const { return new InterfaceBlock(*this); }
213         virtual void visit(NodeVisitor &);
214 };
215
216 struct FunctionDeclaration: Node
217 {
218         std::string return_type;
219         std::string name;
220         std::vector<NodePtr<VariableDeclaration> > parameters;
221         bool definition;
222         Block body;
223
224         FunctionDeclaration();
225
226         virtual FunctionDeclaration *clone() const { return new FunctionDeclaration(*this); }
227         virtual void visit(NodeVisitor &);
228 };
229
230 struct Conditional: Node
231 {
232         NodePtr<Expression> condition;
233         Block body;
234         Block else_body;
235
236         virtual Conditional *clone() const { return new Conditional(*this); }
237         virtual void visit(NodeVisitor &);
238 };
239
240 struct Iteration: Node
241 {
242         NodePtr<Node> init_statement;
243         NodePtr<Expression> condition;
244         NodePtr<Expression> loop_expression;
245         Block body;
246
247         virtual Iteration *clone() const { return new Iteration(*this); }
248         virtual void visit(NodeVisitor &);
249 };
250
251 struct Passthrough: Node
252 {
253         NodePtr<Expression> subscript;
254
255         virtual Passthrough *clone() const { return new Passthrough(*this); }
256         virtual void visit(NodeVisitor &);
257 };
258
259 struct Return: Node
260 {
261         NodePtr<Expression> expression;
262
263         virtual Return *clone() const { return new Return(*this); }
264         virtual void visit(NodeVisitor &);
265 };
266
267 struct NodeVisitor
268 {
269         virtual ~NodeVisitor() { }
270
271         virtual void visit(Block &) { }
272         virtual void visit(Literal &) { }
273         virtual void visit(ParenthesizedExpression &) { }
274         virtual void visit(VariableReference &) { }
275         virtual void visit(MemberAccess &) { }
276         virtual void visit(UnaryExpression &) { }
277         virtual void visit(BinaryExpression &) { }
278         virtual void visit(FunctionCall &) { }
279         virtual void visit(ExpressionStatement &) { }
280         virtual void visit(Layout &) { }
281         virtual void visit(StructDeclaration &) { }
282         virtual void visit(VariableDeclaration &) { }
283         virtual void visit(InterfaceBlock &) { }
284         virtual void visit(FunctionDeclaration &) { }
285         virtual void visit(Conditional &) { }
286         virtual void visit(Iteration &) { }
287         virtual void visit(Passthrough &) { }
288         virtual void visit(Return &) { }
289 };
290
291 struct TraversingVisitor: NodeVisitor
292 {
293         virtual void visit(Block &);
294         virtual void visit(ParenthesizedExpression &);
295         virtual void visit(MemberAccess &);
296         virtual void visit(UnaryExpression &);
297         virtual void visit(BinaryExpression &);
298         virtual void visit(FunctionCall &);
299         virtual void visit(ExpressionStatement &);
300         virtual void visit(StructDeclaration &);
301         virtual void visit(VariableDeclaration &);
302         virtual void visit(InterfaceBlock &);
303         virtual void visit(FunctionDeclaration &);
304         virtual void visit(Conditional &);
305         virtual void visit(Iteration &);
306         virtual void visit(Passthrough &);
307         virtual void visit(Return &);
308 };
309
310 enum StageType
311 {
312         SHARED,
313         VERTEX,
314         GEOMETRY,
315         FRAGMENT
316 };
317
318 struct Stage
319 {
320         StageType type;
321         Stage *previous;
322         ProgramSyntax::Block content;
323         std::map<std::string, VariableDeclaration *> in_variables;
324         std::map<std::string, VariableDeclaration *> out_variables;
325
326         Stage(StageType);
327 };
328
329 struct Module
330 {
331         Stage shared;
332         std::list<Stage> stages;
333
334         Module();
335 };
336
337 } // namespace ProgramSyntax
338 } // namespace GL
339 } // namespace Msp
340
341 #endif