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