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