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